package text.learning;

import java.util.Vector;
import java.util.Hashtable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.StringTokenizer;

import text.ia.TExtAligner;
import text.obj.AlignedChunk;
import text.obj.AlignedSentence;
import text.obj.AlignedTemplate;
import text.obj.ChunkPair;
import text.obj.TExtChunk;
import text.obj.TExtSentence;
import text.obj.TExtTemplate;
import text.util.DBConnector;
import text.util.TExtLearnerUtility;
import text.util.TExtUtility;

public class ChunkRefiner{
	
	private boolean bSourceIsFilipino;
	private TExtLearnerUtility tlu;
	private Vector vTRTrace;
	private Vector vTRContent;
	private Vector vTRTemplates;
	private Vector vTRChunks;
	private Vector vCPObjects;


	
		
	public ChunkRefiner(boolean bSourceIsFilipino1, TExtLearnerUtility tlu) {

		bSourceIsFilipino = bSourceIsFilipino1;
		this.tlu = tlu;
		vTRTrace = new Vector(1);

	}

	/**	This starts the refinement of Template pairs using the given sentence pair (alCurrent).
	 * 
	 * 
	 *	Returns the a primitive boolean to indicate if there are refined template pairs.
	 *
	 *	@param alCurrent a {@link AlignedSentence} representing a sentence pair that will be
	 *		used to refine existing templates in the Templates DB
	 *	@return the primitive boolean that indicates if there are refined template pairs:
	 *		true - there is at least one template pair that is refined by alCurrent
	 *		false - there are no template pairs refined by alCurrent
	 *
	 *	note: parent can be a chunk or a template.
	 *  parent will be null if it came from dttl from DTS
	 *	V: ang input ko ay... a chunk.
	 *     currently assume that input is pure strings.
	 */
	public boolean chunkRefinement(AlignedChunk alCurrent,  
				Vector tcParentS, Vector tcParentT, 
				Hashtable hOldNewDomainMap){
		
		
		println("-------------- Start Refinement: "+ alCurrent);
		
		boolean isSuccessful = false;
		boolean chunkHasNoVars = false;
		String strOldDomain = alCurrent.getSource().domain();
		Vector vMatchingChunks = new Vector(1);
		
		if (alCurrent.getSource().size()>1){

			vMatchingChunks = findMatchingChunks(alCurrent);
			if (hOldNewDomainMap == null) hOldNewDomainMap = new Hashtable();
			
			
			if (vMatchingChunks.size() != 0)
				isSuccessful = refineChunks(alCurrent, vMatchingChunks, hOldNewDomainMap);

			/*
			 * 	if issuccessful , modparent. tcParent is null if it came from DTS
			 *	this means, do this only for TR. in TR, the template/chunk has been written.
			 *	I need parent in TR because i need to search it in DB.
			 */
			
			println("inside CR start refinement");
			println("alCurrent "+alCurrent);
			println("tcParentS " +tcParentS);
			println("tcParentT " +tcParentT);
			println("hOldNewDomainMap "+ hOldNewDomainMap);
			
			if (tcParentS != null && isSuccessful ) {

				
				String strNewDomain = (String)hOldNewDomainMap.get(strOldDomain);
				modifyParentDomainTR(tcParentS, tcParentT, strOldDomain, strNewDomain);
				
			}
		}
		
		
		
		println("-------------- End Refinement: "+ alCurrent);
		
		
		return isSuccessful;
	}
	
	
	
	
	
	//tested. it works.
	public boolean chunkHasNoVars(AlignedChunk alChunk) {
		
		for (Iterator it = alChunk.getSource().iterator(); it.hasNext(); ){
			if (it.next() instanceof TExtChunk) return false;
		}
		
		for (Iterator it = alChunk.getTarget().iterator(); it.hasNext(); ){
			if (it.next() instanceof TExtChunk) return false;
		}
		
		return true;
	}
	
	
	//
	
	
	/**
	 * tested toAlignedSentence. it works.
	 * assumes chunks are plain strings.. empty vars are spelled as domains
	 * 
	 */
	public AlignedSentence toAlignedSentence(AlignedChunk alChunk){
		AlignedSentence alSen;
		Vector vSTokens = new Vector();
		Vector vTTokens = new Vector();
		TExtSentence sSen;
		TExtSentence tSen;
		
		for (Iterator it = alChunk.getSource().iterator(); it.hasNext(); ){
			vSTokens.add(it.next());
		}
		
		for (Iterator it = alChunk.getTarget().iterator(); it.hasNext(); ){
			vTTokens.add(it.next());
		}
		Vector sTemp = new Vector();
		Vector tTemp = new Vector();
		
		sTemp.add(alChunk.getSource());
		tTemp.add(alChunk.getTarget());
		
		Vector vAlignmentHashTemp = getAlignmentInChunks(sTemp, tTemp );
		sSen = new TExtSentence (vSTokens, alChunk.getSource().toString());
		tSen = new TExtSentence (vTTokens, alChunk.getTarget().toString());
		
		alSen = new AlignedSentence(sSen, tSen, (Hashtable)vAlignmentHashTemp.get(2) );
		alSen.setSourceToFilipino(bSourceIsFilipino);
		
		return alSen;
	}
	
	
	
	
	
	
	
	
	
	/**
	 *  Vince new code. for modifying parents...
	 *  but this should be done outside the class.
	 * 
	 */ 
	private void modifyParentDomain(Object ctParentS, Object ctParentT, 
		String strOldDomain, String strNewDomain) {
		println("153 CODE1 modify parent domain was called");
		String strSource = new String();
		String strTarget = new String();
		String strQuery = new String();
		String strTable = "Template";
		
		if(bSourceIsFilipino){
			strSource="Filipino";
			strTarget="English";
		}else{
			strSource="English";
			strTarget="Filipino";
		}
		
		
		if (ctParentS instanceof TExtTemplate && ctParentT instanceof TExtTemplate){
			TExtTemplate tSourceRefinement = (TExtTemplate)ctParentS;
			TExtTemplate tTargetRefinement = (TExtTemplate)ctParentT;
			strTable = "Template";
			strQuery = "SELECT templateID FROM Templates WHERE "
				+ "template" + strSource + " = '" 
				+ tSourceRefinement.toString().replace("'","''") + "'"
				+ " AND template" + strTarget + " = '" 
				+ tTargetRefinement.toString().replace("'","''") + "'"
				+ " AND compID" + strSource + " = '" 
				+ tSourceRefinement.getCompoundIdentifier() + "'"
				+ " AND compID" + strTarget + " = '" 
				+ tTargetRefinement.getCompoundIdentifier() + "'";
		}
		  

			  
		if (ctParentS instanceof TExtChunk && ctParentT instanceof TExtChunk){
			TExtChunk cSourceRefinement = (TExtChunk)ctParentS;
			TExtChunk cTargetRefinement = (TExtChunk)ctParentT;
			strTable = "Chunk";
			strQuery = "SELECT chunkID FROM Chunks WHERE "
				+ "chunk" + strSource + " = '" 
				+ cSourceRefinement.toString().replace("'","''") + "'"
				+ " AND domain LIKE '"+ cSourceRefinement.domain()+"' "
				+ " AND chunk" + strTarget + " = '" 
				+ cTargetRefinement.toString().replace("'","''") + "'"
				+ " AND compID" + strSource + " = '" 
				+ cSourceRefinement.getCompoundIdentifier() + "'"
				+ " AND compID" + strTarget + " = '" 
				+ cTargetRefinement.getCompoundIdentifier() + "'";
		}

		
		
		ResultSet rs;
		
		rs = DBConnector.select(strQuery);
		
			boolean x = true;
			String strTempS = new String();
			String strTempT = new String();
			String strID = new String();
			
			strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
			strTempT = ctParentT.toString();
			
			try{
				if (rs.next()) {
					strID = rs.getString(strTable+"ID");
					strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
					strTempT = ctParentT.toString();
				}
				rs.close();
			}catch(SQLException s){
				System.out.println("EXCEPTION IN MODPARENT DOMAIN");
				s.printStackTrace();
			}
			
			strTempS = strTempS.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");
			strTempT = strTempT.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");

			for(Iterator it = ((Vector)ctParentT).iterator(); it.hasNext();  ){
				Object o =  it.next();
				if (o instanceof TExtChunk) {
					String strCTDom = ((TExtChunk)o).domain(); //[3] [3/4.2]
					strCTDom = stripSquareBrackets(strCTDom);
					//to see if [3.1] belongs to [3]
					if (domainIsPartOf(strCTDom,strOldDomain ) ){
						strTempS = strTempS.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
						strTempT = strTempT.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
					}
				}
			}
			
			strTempS = tlu.generalizeDomains(strTempS);
			strTempT = tlu.generalizeDomains(strTempT);
			
			//end v
				
			//always [91/35.1] [92] is already in DB! 
			//how do i modify always [91] [92].. sino nagmodify nito to that
			
			//chunk +s = "chunks" = the table
			
			if (strTempT.equals("")) return;
				
				
			String strUpdate = "UPDATE "+ strTable +"s SET " 
					+ strTable+strSource + " ='"
					+ strTempS.trim().replace("'","''") + "', "
					+ strTable+strTarget +  "= '" 
					+ strTempT.trim().replace("'","''") + "' "
					+ "WHERE "+ strTable + "ID = '"+ strID + "'";
				
			
			if (!strTempS.trim().equals(ctParentS))
			DBConnector.update(strUpdate);
			//THIS SHOULD NOT FAIL ^
			
			if(ctParentS instanceof TExtTemplate){
				TExtTemplate tSource = new TExtTemplate(strTempS,((TExtTemplate)ctParentS).getCompoundIdentifier() );
				TExtTemplate tTarget = new TExtTemplate(strTempT,((TExtTemplate)ctParentT).getCompoundIdentifier() );
				
				
				((TExtTemplate)ctParentS).clear();
				((TExtTemplate)ctParentT).clear();
				
				((TExtTemplate)ctParentS).addAll(tSource);
				((TExtTemplate)ctParentT).addAll(tTarget);
			}
			
			if(ctParentS instanceof TExtChunk){
				TExtChunk cSource = new TExtChunk(strTempS, ((TExtChunk)ctParentS).domain() );
				TExtChunk cTarget = new TExtChunk(strTempT, ((TExtChunk)ctParentT).domain() );
				
				cSource.setCompoundIdentifier( ((TExtChunk)ctParentS).getCompoundIdentifier());
				cSource.adjustChunkCompound();
				cTarget.setCompoundIdentifier( ((TExtChunk)ctParentT).getCompoundIdentifier());
				cTarget.adjustChunkCompound();
				
				((TExtChunk)ctParentS).clear();
				((TExtChunk)ctParentT).clear();
				
				((TExtChunk)ctParentS).addAll(cSource);
				((TExtChunk)ctParentT).addAll(cTarget);
				
			}
	
	}
	
	

	/**
	 *  Vince new code. for modifying parents...
	 *  but this should be done outside the class.
	 *  
	 *  will add the domains in strNewDomain to the appropriate domain set
	 *  in the parent
	 * 
	 */ 
	public void modifyParentDomainTR(Object ctParentS, Object ctParentT, 
		String strOldDomain, String strNewDomain) {
		String strSource = new String();
		String strTarget = new String();
		String strQuery = new String();
		String strTable = "Template";
		
		if(bSourceIsFilipino){
			strSource="Filipino";
			strTarget="English";
		}else{
			strSource="English";
			strTarget="Filipino";
		}
		
		
		if (ctParentS instanceof TExtTemplate && ctParentT instanceof TExtTemplate){
			TExtTemplate tSourceRefinement = (TExtTemplate)ctParentS;
			TExtTemplate tTargetRefinement = (TExtTemplate)ctParentT;
			strTable = "Template";
			strQuery = "SELECT templateID FROM Templates WHERE "
				+ "template" + strSource + " = '" 
				+ tSourceRefinement.toString().replace("'","''") + "'"
				+ " AND template" + strTarget + " = '" 
				+ tTargetRefinement.toString().replace("'","''") + "'"
				+ " AND compID" + strSource + " = '" 
				+ tSourceRefinement.getCompoundIdentifier() + "'"
				+ " AND compID" + strTarget + " = '" 
				+ tTargetRefinement.getCompoundIdentifier() + "'";
		}
		  

			  
		if (ctParentS instanceof TExtChunk && ctParentT instanceof TExtChunk){
			TExtChunk cSourceRefinement = (TExtChunk)ctParentS;
			TExtChunk cTargetRefinement = (TExtChunk)ctParentT;
			strTable = "Chunk";
			strQuery = "SELECT chunkID FROM Chunks WHERE "
				+ "chunk" + strSource + " = '" 
				+ cSourceRefinement.toString().replace("'","''") + "'"
				+ " AND domain LIKE '"+ cSourceRefinement.domain()+"' "
				+ " AND chunk" + strTarget + " = '" 
				+ cTargetRefinement.toString().replace("'","''") + "'"
				+ " AND compID" + strSource + " = '" 
				+ cSourceRefinement.getCompoundIdentifier() + "'"
				+ " AND compID" + strTarget + " = '" 
				+ cTargetRefinement.getCompoundIdentifier() + "'";
		}

		
		
		ResultSet rs;
		
		rs = DBConnector.select(strQuery);
		
			boolean x = true;
			String strTempS = new String();
			String strTempT = new String();
			String strID = new String();
			
			strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
			strTempT = ctParentT.toString();
			
			try{
				if (rs.next()) {
					strID = rs.getString(strTable+"ID");
					strTempS = ctParentS.toString(); //chunkEnglish or templateEnglish
					strTempT = ctParentT.toString();
				}
				rs.close();
			}catch(SQLException s){
				System.out.println("EXCEPTION IN MODPARENT DOMAIN");
				s.printStackTrace();
			}
			
			strTempS = strTempS.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");
			strTempT = strTempT.replace("["+strOldDomain+"]" , "["+strNewDomain+"]");

			for(Iterator it = ((Vector)ctParentT).iterator(); it.hasNext();  ){
				Object o =  it.next();
				if (o instanceof TExtChunk) {
					String strCTDom = ((TExtChunk)o).domain(); //[3] [3/4.2]
					strCTDom = stripSquareBrackets(strCTDom);
					//to see if [3.1] belongs to [3]
					if (domainIsPartOf(strCTDom,strOldDomain ) ){
						strTempS = strTempS.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
						strTempT = strTempT.replace("["+strCTDom+"]" , "["+strCTDom+"/"+strNewDomain+"]");
					}
				}
			}
			/*
			 * generalization is not done in TR chunkRefinement. it should be done in the updateDB
			 * but i can remove duplicates here so... i will
			strTempS = generalizeDomains(strTempS);
			strTempT = generalizeDomains(strTempT);
			*/
			
			strTempS = TExtLearnerUtility.removeDuplicateDomains(strTempS);
			strTempT = TExtLearnerUtility.removeDuplicateDomains(strTempT);
			
			//chunk +s = "chunks" = the table
			
			if (strTempT.equals("")) return;
				
				
			String strUpdate = "UPDATE "+ strTable +"s SET " 
					+ strTable+strSource + " ='"
					+ strTempS.trim().replace("'","''") + "', "
					+ strTable+strTarget +  "= '" 
					+ strTempT.trim().replace("'","''") + "' "
					+ "WHERE "+ strTable + "ID = '"+ strID + "'";
				
			
			if (!strTempS.trim().equals(ctParentS))
			DBConnector.update(strUpdate);
			
			if(ctParentS instanceof TExtTemplate){
				TExtTemplate tSource = new TExtTemplate(strTempS,((TExtTemplate)ctParentS).getCompoundIdentifier() );
				TExtTemplate tTarget = new TExtTemplate(strTempT,((TExtTemplate)ctParentT).getCompoundIdentifier() );
				
				
				((TExtTemplate)ctParentS).clear();
				((TExtTemplate)ctParentT).clear();
				
				((TExtTemplate)ctParentS).addAll(tSource);
				((TExtTemplate)ctParentT).addAll(tTarget);
			}
			
			if(ctParentS instanceof TExtChunk){
				TExtChunk cSource = new TExtChunk(strTempS, ((TExtChunk)ctParentS).domain() );
				TExtChunk cTarget = new TExtChunk(strTempT, ((TExtChunk)ctParentT).domain() );
				
				cSource.setCompoundIdentifier( ((TExtChunk)ctParentS).getCompoundIdentifier());
				cSource.adjustChunkCompound();
				cTarget.setCompoundIdentifier( ((TExtChunk)ctParentT).getCompoundIdentifier());
				cTarget.adjustChunkCompound();
				
				((TExtChunk)ctParentS).clear();
				((TExtChunk)ctParentT).clear();
				
				((TExtChunk)ctParentS).addAll(cSource);
				((TExtChunk)ctParentT).addAll(cTarget);
				
			}
	
	}
	
	
	
	
	
	
	
	/**
	 * Checks if the domain is a part of that domain or is that domain.
	 * example: returns true for 98, 98.3
	 * false for 98, 85.4
	 * true for 95.5, 95.5
	 * 
	 * @param strDomainReference
	 * @param strToBeChecked
	 * @return
	 */
	public boolean domainIsPartOf(String strDomainReference, String strToBeChecked){
		if(strDomainReference.equals(strToBeChecked)) return true;
		
		if(TExtUtility.compareBaseDomain(strDomainReference, strToBeChecked)) return true;
		
		return false;
	}
	
	
	
	
	
	
	
	/**
	 * finds matches for the specified aligned chunk. should accept
	 * chunks with vars, ignore var.
	 * 
	 * adds the tokens in the chunk in a vToken, variables are ignored.
	 * 
	 * taken from TExtTemplateRefiner
	 * should only be used for deriveTemplate like template refinement
	 * 
	 * @param alChunk
	 * @return vector of chunk matches 
	 * 
	 * TEST this func
	 */
	protected Vector findMatchingChunks(AlignedChunk alTemp) {
		Vector vMatchingTemplates = new Vector(1); // this is used for chunks now
		
		String strStmt = new String();
		ResultSet rsTemplates;

		
		TExtChunk senSource = alTemp.getSource();
		TExtChunk senTarget = alTemp.getTarget();
		int nTargetWordSize = senTarget.size();
		
		String strSourceTable;
		String strTargetTable;
		String strSCompID;
		String strTCompID;
		
		if(bSourceIsFilipino) {
			strSourceTable = "chunkFilipino";
			strTargetTable = "chunkEnglish";
			strTCompID = "compIDEnglish";
			strSCompID = "compIDFilipino";
		}
		else {
			strSourceTable = "chunkEnglish";
			strTargetTable = "chunkFilipino";
			strSCompID = "compIDEnglish";
			strTCompID = "compIDFilipino";
		}
			
		strStmt = "Select * From Chunks Where " + strSourceTable;
		
		
		// do i need to filter or get strings only?
		//Vector vTemp = senSource.getWords();
		Vector vTemp = new Vector();
		for (Iterator i = senSource.iterator(); i.hasNext();){
			Object o = i.next();
			if (o instanceof String) vTemp.add(o);
		}
		
		
		int nLimitS = vTemp.size();

		
		Vector vAlReady = new Vector(5);
		for(int a=0; a<nLimitS; a++) {
			String strWord = (String)vTemp.get(a);
			String strSearch;
			
			if(vAlReady.contains(strWord.toLowerCase()))
				continue;
			vAlReady.add(strWord.toLowerCase());
			
			if(a == 0)
				strSearch = strWord.replace("'","''") + " %";
			else if(a == nLimitS - 1)
				strSearch = "% " + strWord.replace("'","''");
			else
				strSearch = "% " + strWord.replace("'","''") + " %";
			
			rsTemplates = DBConnector.select(strStmt + " Like '" +
				strSearch +"'");
			
			strSearch = null;
			
			try {
				while(rsTemplates.next()) {
					boolean bMatchFound = false;
					String strTargetT = rsTemplates.getString(strTargetTable);
					String strSourceT = rsTemplates.getString(strSourceTable);
					String strTargetID = rsTemplates.getString(strTCompID);
					String strSourceID = rsTemplates.getString(strSCompID);
					String strDomain = rsTemplates.getString("domain");
					
					for(int b=0; !bMatchFound && b<nTargetWordSize; b++) {
						String strCheck = (String)senTarget.get(b).toString();
						if(strTargetT.toLowerCase().contains(strCheck.toLowerCase()))
							bMatchFound = true;
					}
					if(bMatchFound) {
						
						boolean bCanInclude = true;

						
						for(int i=0; i < vMatchingTemplates.size(); i++) {
							AlignedChunk alc = (AlignedChunk) vMatchingTemplates.get(i);
							
							if(    alc.getSource().toString().equals(strSourceT)
									&& alc.getTarget().toString().equals(strTargetT) 
									&& alc.getSource().getCompoundIdentifier().equals(strSourceID)
									&& alc.getTarget().getCompoundIdentifier().equals(strTargetID) ){
								bCanInclude = false;	
								break;
							}	else{
								bCanInclude = true;

							}

						}
						
						if(bCanInclude || vMatchingTemplates.isEmpty()){
							TExtChunk cSource = tlu.adjustChunkCompound(new TExtChunk(strSourceT, strDomain, strSourceID));
							TExtChunk cTarget = tlu.adjustChunkCompound(new TExtChunk(strTargetT, strDomain, strTargetID));
							vMatchingTemplates.add(new AlignedChunk(cSource, cTarget));
						}

					}
				}
			} catch(SQLException sqle) {
				System.err.println("Template Result Set error: "+sqle.getMessage());
			}
		}
//
		strStmt = null;
		rsTemplates = null;
		vAlReady = null;
//
		return vMatchingTemplates;
	}
	
	public Vector getTRTrace(){
		return vTRTrace;
	}
	

	/**	Given the Sentence pair (alTemp) and the Template pairs having common tokens with
	 *		alTemp, this initiates the refinement for every pairing of alTemp with each of
	 *		the Template pairs.
	 */
	protected boolean refineChunks(AlignedChunk alTemp, 
			Vector vMatchingTemplates, 
			Hashtable hOldNewDomainMap){

		
		char cPunc;
		int nCount = 0;
		String strOrigDomain = alTemp.getSource().domain();
		String strNewDomain = new String();
	
		//v note: vMatchingTemplates now contains fully formed AlignedChunks
		for(Iterator it = vMatchingTemplates.iterator(); it.hasNext(); ) {
			String strAdditionalDomain = null;
			//the matching aligned chunk
			AlignedChunk alTemplate = (AlignedChunk)it.next();
			
			//ALGO FILTERS
			if(!chunkHasNoVars(alTemplate)){
				
				//TR algorithm
				println("went to TR with "+alTemplate);
				alTemp.getSource().adjustForChunkLearning();
				alTemp.getTarget().adjustForChunkLearning();
				Vector vDerived = deriveChunkTR(alTemplate, alTemp);
				
				if(vDerived != null) {
					strAdditionalDomain = ((AlignedChunk)vDerived.get(0)).getSource().domain();
					

					//prevent adding 27.1 if there is 27
					if (TExtUtility.compareBaseDomain(strNewDomain, strAdditionalDomain)){				
					}else{
						if (strNewDomain.equals("")) strNewDomain=strAdditionalDomain;
						else strNewDomain+="/"+strAdditionalDomain;					
					}
					
					vTRTrace.add("TR");
					vTRContent = new Vector(1);
					vTRContent.add(alTemp);
					vTRTemplates = new Vector(1);
					vTRChunks = new Vector(1);
					updateDB((AlignedChunk)vDerived.get(0), (AlignedChunk)vDerived.get(1), 
						(Vector)vDerived.get(2), (Vector)vDerived.get(3), 
						((Boolean)vDerived.get(4)).booleanValue(), (Hashtable)vDerived.get(5),
						((Boolean)vDerived.get(6)).booleanValue());
					vTRContent.add(vTRTemplates);
					vTRContent.add(vTRChunks);
					vTRTrace.add(vTRContent);
					nCount++;

				}
				vDerived = null;
				
			}//end TR 
			else
			if (chunkHasNoVars(alTemplate) && chunkHasNoVars(alTemp) ) {
				//DTS algorithm
				println("went to DTS with "+ alTemplate);
				boolean b = deriveChunkSimDiff(alTemp, alTemplate);
				println("428 derived: "+ b);
				if (b) {
					nCount++; // so that refinement will return success.
					strAdditionalDomain= alTemplate.getSource().domain();
					
					if (TExtUtility.compareBaseDomain(strNewDomain, strAdditionalDomain)){				
					}else{
						if (strNewDomain.equals("")) strNewDomain=strAdditionalDomain;
						else strNewDomain+="/"+strAdditionalDomain;					
					}
				}
			}else{
				//DTC algo
			}//end DTS
				
				
		
		} // end for
		
		System.out.println("sta hOldNewDomainMap "+  hOldNewDomainMap);	
		if (hOldNewDomainMap!= null) hOldNewDomainMap.put(strOrigDomain, strNewDomain );
		System.out.println("fin hOldNewDomainMap "+  hOldNewDomainMap);
		
		if(nCount == 0)
			return false;
		return true;
				
			
	}
			
			
		


//SAME
	/**	This compares the tokens of the given sentence (senTemp) and the candidate
	 *		template (temMatch) to get similar tokens between senTemp and temMatch.
	 *
	 *	being modified v. change to chunk
	 */
	protected Vector getSimilarTokens(TExtChunk senTemp, TExtChunk temMatch){
		boolean hasMatch = false;
		String strMatch;
		String strComp;
		Vector vMatches = new Vector(1);
		
		int c = 0;
		
		int nSenTempSize = senTemp.size();
		int nTemMatchSize = temMatch.size();
		
		for(int d=0; d<nSenTempSize; d++) {
			strComp = (String)senTemp.get(d).toString();
			hasMatch = false;
			for(int b=c; !hasMatch && b<nTemMatchSize; b++) {
				strMatch = (temMatch.get(b)).toString();
				if(strComp.compareToIgnoreCase(strMatch) == 0){
					hasMatch = true;
					c = b;
					do {
						vMatches.add(strComp);
						d++; c++;
						if(d>=nSenTempSize || c>=nTemMatchSize)
							break;
						strComp = (String)senTemp.get(d).toString();
						strMatch = (temMatch.get(c)).toString();
					} while(strComp.compareToIgnoreCase(strMatch) == 0);
					c++;
				}
			}
		}
		return vMatches;
	}
//SAME EXCEPT FOR "RETURN"
	/**	Checks whether the Match Sequence (sequence of Similarities and Differences) of the
	 *		given sentence (senTemp) and the given template (temMatch) are the same
	 *		by using the Vector of similar tokens between them.
	 *
	 *	v. change to chunk
	 */
	protected Vector checkPattern(TExtChunk senTemp, TExtChunk temMatch, Vector vMatches){
		Vector vTemplateTempS = new Vector();
		boolean lastWasChunk = false;
		String strReturn = "true";
		int h = 0;
		int nMatchCtr = 0;
		
		int nTempChunkCtr1 = 0;
		int nTempChunkCtr2 = 0;
		
		int nSenTempSize = senTemp.size();
		int nMatchesSize = vMatches.size();
		
		for(int f=0; f<nSenTempSize; f++)
			if(nMatchCtr < nMatchesSize && (vMatches.get(nMatchCtr)).toString().equalsIgnoreCase(senTemp.get(f).toString())){
				System.out.print("String");
				vTemplateTempS.add(new String());
				lastWasChunk = false;
				nMatchCtr++;
			}
			else {
				if(!lastWasChunk) {
					System.out.print("Chunk");
					nTempChunkCtr1++;
					vTemplateTempS.add(new TExtChunk());
				}
				lastWasChunk = true;
			}
			
//		System.out.println();
		nMatchCtr = 0;
		lastWasChunk = false;
		int nTemMatchSize = temMatch.size();
		for(int g=0; g<nTemMatchSize; g++){
			if(nMatchCtr < nMatchesSize && (vMatches.get(nMatchCtr)).toString().equalsIgnoreCase(temMatch.get(g).toString())){
				System.out.print("String");
				if(lastWasChunk)
					h++;
				if(h == vTemplateTempS.size()){
					strReturn = "false";
					break;
				}
				if(vTemplateTempS.get(h) instanceof TExtChunk)
					strReturn = "false";
				lastWasChunk = false;
				h++;
				nMatchCtr++;
			} else {
				if(!lastWasChunk){
					System.out.print("Chunk");
					nTempChunkCtr2++;
				}
				if(h == vTemplateTempS.size()){
					strReturn = "false";
					break;
				}
				if(vTemplateTempS.get(h) instanceof String){
					strReturn = "false";
					System.out.print("NOT MATCH!!!");					
				}
				lastWasChunk = true;
			}
		}
		if(nTempChunkCtr2 != nTempChunkCtr1)
			strReturn = "false";
//		System.out.println();

		vTemplateTempS.add(0, strReturn);
		return vTemplateTempS;
	}

//SAME
	/**	The Differences (sequence of tokens not common in the given sentence and
	 *		template) or chunks that will be learned are constructed and the initial
	 *		new template is also constructed as a StringBuffer wherein the
	 *		Differences are enclosed in square brackets.
	 */
	protected Vector constructChunks(Vector vTokens, Vector vMatches, String strCompoundIdentifier, int nRetNextChunk){
		Vector vChunks = new Vector();
		StringBuffer sbTemp = new StringBuffer();
		Object token;
		boolean isInHashTable = false;
		TExtChunk cTemp;
		Object otemp;
		Vector vTempChunk = null;
		String strNextChunk = null;
		int nChunkTracker;
		boolean lastWasChunk = false;
		int nMatchCtr = 0;		
		
		int nLBound = 0;
		int nUBound;
		Vector vRemoved = new Vector(3);
		Vector vRemoved2;
		int nStart = -1;
		nChunkTracker = 0;
		
		int nMatchesSize = vMatches.size();

		int nTokens = vTokens.size();
		for(int l=0; l<nTokens; l++){
			token = vTokens.get(l);
			if(nMatchCtr < nMatchesSize && (vMatches.get(nMatchCtr)).toString().equalsIgnoreCase(token.toString())){
				if(lastWasChunk){
					vRemoved.add(nStart + "-" + (nLBound-1));
					sbTemp.deleteCharAt(sbTemp.length()-1);
					sbTemp.append("]");
					sbTemp.append(" ");
					if(vTempChunk.size() == 1){
						otemp = vTempChunk.get(0);
						if(otemp instanceof TExtChunk){
							vChunks.add(otemp);
							nChunkTracker--;				
						}
						else{
							cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
							cTemp = tlu.adjustChunkCompound(cTemp);
							vChunks.addElement(cTemp);
						}
					}
					else{
						cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
						cTemp = tlu.adjustChunkCompound(cTemp);
						vChunks.addElement(cTemp);
					}
				}
				nMatchCtr++;
				sbTemp.append(token);
				sbTemp.append(" ");
				if(token instanceof String && token.toString().contains(" "))
					nLBound += (new StringTokenizer(token.toString())).countTokens()-1;	
				lastWasChunk = false;
			} else{
				if(!lastWasChunk){
					nStart = nLBound;
					vTempChunk = new Vector(3);
					sbTemp.append("[");
					strNextChunk = ""+(nRetNextChunk+nChunkTracker);
					nChunkTracker++;
				}
				sbTemp.append(token);
				if(!(strCompoundIdentifier.equals("none")) && 
					token instanceof String && token.toString().contains(" ")){
						nUBound = nLBound + (new StringTokenizer(token.toString())).countTokens()-1;
						nLBound = nUBound;
				}
				sbTemp.append(" ");
				vTempChunk.addElement(token);
				lastWasChunk = true;
			}
			nLBound++;
		}
		if(lastWasChunk){
			vRemoved.add(nStart + "-" + (nLBound-1));
			sbTemp.deleteCharAt(sbTemp.length()-1);
			String sbTempTemp = new String(sbTemp);
			sbTemp.append("]");
			sbTemp.append(" ");
			if(vTempChunk.size() == 1){
				otemp = vTempChunk.get(0);
				if(otemp instanceof TExtChunk){
					vChunks.add(otemp);
					nChunkTracker--;					
				}
				else{
					cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
					cTemp = tlu.adjustChunkCompound(cTemp);
					vChunks.addElement(cTemp);
				}
			}
			else{
				cTemp = new TExtChunk(tlu.toStringChunk(vTempChunk), "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
				cTemp = tlu.adjustChunkCompound(cTemp);
				vChunks.addElement(cTemp);
			}
		}
		vChunks.add(sbTemp);
		
		if(!(strCompoundIdentifier.equals("none"))){
			vRemoved2 = tlu.editCompID(strCompoundIdentifier, vRemoved);
			strCompoundIdentifier = tlu.adjustCompIDVal(tlu.removeCompID(strCompoundIdentifier, vRemoved2), vRemoved);
			vChunks.add(strCompoundIdentifier);		
		}
		return vChunks;
	}

//SAME	
	/**	Constructs the new template by replacing the Differences (identified chunks) with their
	 *		corresponding domains.
	 */
	protected StringBuffer constructTemplate(String strTemp, Vector vAlChunk2){
		Vector vAlChunk = (Vector)vAlChunk2.clone();
		StringBuffer sbChunk = new StringBuffer();
		boolean bChunkScan = false;
		String strChunkTemp;
		int nOpen = 0;
		int nClose = 0;
		
		StringBuffer sbTemplate = new StringBuffer();
		
		int nTempLength = strTemp.length();
		for(int p=0; p<nTempLength; p++) {
			if(!bChunkScan)
				sbTemplate.append(strTemp.charAt(p));
			else
				sbChunk.append(strTemp.charAt(p));
			if(strTemp.charAt(p) == '[')
				nOpen++;
			if(strTemp.charAt(p)=='[' && !bChunkScan) {
				sbChunk = new StringBuffer();
				sbChunk.append(strTemp.charAt(p));
				bChunkScan = true;
			} else if(strTemp.charAt(p)==']') {
				nClose ++;
				if(nOpen == nClose){
					StringBuffer sbOpen = new StringBuffer();
					StringBuffer sbClose = new StringBuffer();
					for(int nCtr = 0; nCtr < nOpen; nCtr++){
						sbOpen.append("[");
						sbClose.append("]");
					}
					nOpen = 0;
					nClose = 0;
					for(int q=0; q<vAlChunk.size(); q++) {
						TExtChunk cCheck = (TExtChunk)vAlChunk.get(q);
						strChunkTemp = cCheck.toString();
						if(!(strChunkTemp.startsWith(sbOpen.toString()) && strChunkTemp.endsWith(sbClose.toString())))
							strChunkTemp = "[" + strChunkTemp + "]";
						if(strChunkTemp.equals(sbChunk.toString())) {
							vAlChunk.remove(q);
							q--;
							
							if(cCheck.domain().startsWith("[") && cCheck.domain().endsWith("]"))
								sbTemplate.append(cCheck.domain().substring(1, cCheck.domain().length()-1));
							else
								sbTemplate.append(cCheck.domain());
							break;
						}
					}
					sbTemplate.append(strTemp.charAt(p));
					bChunkScan = false;
				}
			}
		}
//
		vAlChunk = null;
		sbChunk = null;
//
		return sbTemplate;	
	}

	/**	Adjust the StringBuffer representation of the refinement (new template)
	 *		such that chunks like "[[1.1]]" will be converted to "[1.1]".
	 */
	protected StringBuffer adjustTemplateChunks(StringBuffer templateTemp){
		int nStart;
		int nEnd;
		String strMiddle = null;
		
		nStart = templateTemp.indexOf("[[");
		nEnd = templateTemp.indexOf("]]", nStart);
		while(nStart != -1 && nEnd != -1){
			strMiddle = templateTemp.substring(nStart+1, nEnd+1);
			if(!(strMiddle.contains(" "))){
				templateTemp = templateTemp.replace(nStart, nEnd+2, strMiddle);
			}
			nStart = templateTemp.indexOf("[[", nEnd+1);
			nEnd = templateTemp.indexOf("]]", nStart);
		}
		strMiddle = null;
		return templateTemp;
	}

	/**	This adjusts the domain of the aligned Source and Target Chunks such that their
	 *		domains must be the same.
	 */
	protected Vector adjustDomain(Vector vSource, Vector vTarget){
		TExtChunk tempS;
		TExtChunk tempT;
		Vector vSourceFinal = new Vector(vSource.size());
		Vector vTargetFinal = new Vector(vTarget.size());
		int nLargest = 0;
		Vector vSourceDomain = new Vector(vSource.size());
		Vector vTargetDomain = new Vector(vTarget.size());		
		
		int nSourceCount = 0;
		

		for(Iterator s = vSource.iterator(); s.hasNext(); )
			if(((TExtChunk)s.next()).size() == 0)
				nSourceCount++;
		
		if(nSourceCount == vSource.size()){
			try{
				nLargest = Integer.parseInt(tlu.returnNextChunk()) - 1;
			}
			catch(Exception e){
				
			}
		}
		else{
			for(Iterator s = vSource.iterator(); s.hasNext(); ){
				TExtChunk chunkTemp = (TExtChunk)s.next();
				if(chunkTemp.size() > 0){
					try{
						nLargest = Integer.parseInt((chunkTemp.domain()).substring(1, (chunkTemp.domain()).length()-1));
					}
					catch(Exception e){
					}
				}
			}
		}

		for(Iterator s = vSource.iterator(), t = vTarget.iterator(); s.hasNext() && t.hasNext(); ){
			tempS = (TExtChunk)s.next();
			tempT = (TExtChunk)t.next();
			if(tempS.size() > 0 && tempT.size() == 0 ||
				tempS.size() > 0 && tempT.size() > 0){
					tempT.setDomain(tempS.domain());
			}
			else if(tempS.size() == 0 && tempT.size() == 0){
				String strSDom = tempS.domain();
				String[] strSChunkDom = (strSDom.substring(1, strSDom.length()-1)).split("/");
				boolean bHasWhole = false;
				int nDomCount = strSChunkDom.length;
				for(int i = 0; i < nDomCount && !bHasWhole; i++)
					if(!(strSChunkDom[i].contains(".")))
						bHasWhole = true;
				if(!bHasWhole){
					nLargest++;
					tempS.setDomain(new String("[" + nLargest + "/" + strSDom.substring(1, strSDom.length()-1) + "]"));
				}
				tempT.setDomain(tempS.domain());
			}
			else if(tempS.size() == 0 && tempT.size() > 0){
				nLargest++;
				tempS.setDomain(new String("[" + nLargest + "]"));
				tempT.setDomain(tempS.domain());
			}
			if(tempS.size() == 0)
				vSourceDomain.add(tempS.domain());
			if(tempT.size() == 0)
				vTargetDomain.add(tempT.domain());
			vSourceFinal.add(tempS);
			vTargetFinal.add(tempT);
		}

//
		vSource = null;
		vTarget = null;
//
		Vector vFinal = new Vector(4);
		vFinal.add(vSourceFinal);
		vFinal.add(vTargetFinal);
		vFinal.add(vSourceDomain);
		vFinal.add(vTargetDomain);
		return vFinal;
	}	

	/**	Given a Vector of chunks, this function converts these chunks to String form
	 *		together with its domain and compound identifier and place it in a single
	 *		Vector.
	 *	This returns the Vector containing the String forms of the chunks.
	 */
	protected Vector toStringVector(Vector vChunks){
		Vector vAlChunkString = new Vector(3);
		TExtChunk transfer;
		for(Iterator it = vChunks.iterator(); it.hasNext(); ){
			transfer = (TExtChunk)it.next();
			vAlChunkString.add(transfer.toString());
			vAlChunkString.add(transfer.domain());
			vAlChunkString.add(transfer.getCompoundIdentifier());
		}
		return vAlChunkString;
	}
	
	/**	After aligment of chunks, the chunks would not be in their original order.
	 *	This function simply gets the relation (based on indices) of the new order
	 *		of chunks (vNewOrdeR) to the original order of chunks (vOriginalOrder)
	 */
	protected Vector arrangeOrder(Vector vOriginalOrder, Vector vNewOrder, boolean isForBuffer){
		int nIndex2;
		TExtChunk tempChunk;
		Vector nTargetDomain = new Vector(vOriginalOrder.size());
		
		for(Iterator it = vNewOrder.iterator(); it.hasNext(); ){
			TExtChunk tempChunk2 = (TExtChunk)it.next();
			nIndex2 = -1;
			for(Iterator it2 = vOriginalOrder.iterator(); it2.hasNext(); ){
				tempChunk = (TExtChunk)it2.next();
				if(isForBuffer && tempChunk.size() == 0)
					nIndex2++;
				else if(!isForBuffer)
					nIndex2++;
				if(isForBuffer && tempChunk.size() == 0 && 
					tempChunk.toString().equals(tempChunk2.toString()) && 
					tempChunk.domain().equals(tempChunk2.domain()) && 
					!(nTargetDomain.contains(new Integer(nIndex2)))) {
						
					nTargetDomain.add(new Integer(nIndex2));
					break;
				}
				if(!isForBuffer && tempChunk.toString().equals(tempChunk2.toString()) && 
					tempChunk.domain().equals(tempChunk2.domain()) && 
					!(nTargetDomain.contains(new Integer(nIndex2)))) {
					
					nTargetDomain.add(new Integer(nIndex2));
					break;
				}
			}
		}
		return nTargetDomain;
	}
	
	/**	This function modifies the StringBuffer representation of the new template
	 *		specifically the chunks that will be retained (chunks containing domains
	 *		only e.g. [1]) after the chunks, specifically their domains, have been adjusted.
	 */
	protected StringBuffer mapDomain(Vector vDomain, StringBuffer sbTemp, Vector nDomainOrder){
		int nDomainSize = vDomain.size();
		int nChunkPlace;
		String strTempChunk;
		StringTokenizer st = new StringTokenizer(sbTemp.toString());
		String[] strTemplateBuffer = new String[st.countTokens()];
		int nCtr=0;
		while(st.hasMoreTokens()){
			strTemplateBuffer[nCtr] = (String)st.nextToken();
			nCtr++;
		}
//		
		st = null;
//
		int nOpen = 0;
		int nClose = 0;
		
		for(int k = 0; k < nDomainSize; k++){
			nChunkPlace = ((Integer)nDomainOrder.get(k)).intValue();
			int nChunk = -1;
			for(int m = 0; m < strTemplateBuffer.length; m++){
				strTempChunk = strTemplateBuffer[m];
				int nOpenTemp = 0;
				int nCloseTemp = 0;
				if(strTempChunk.contains("[") || strTempChunk.contains("]")){
					for(int n = 0; n < strTempChunk.length(); n++)
						if(strTempChunk.charAt(n) == '['){
							nOpen++;
							nOpenTemp++;
						}
						else if(strTempChunk.charAt(n) == ']'){
							nClose++;
							nCloseTemp++;
						}
				}
				if(nOpen == nClose && nOpenTemp == nCloseTemp && 
					strTempChunk.startsWith("[") && strTempChunk.endsWith("]")){
					
					String strMiddle = strTempChunk.substring(1, strTempChunk.length()-1);
					boolean isNumber = true;
					int nMiddle = strMiddle.length();
					for(int n = 0; n < nMiddle; n++)
						if(!(Character.isDigit(strMiddle.charAt(n))) && strMiddle.charAt(n) != '.' && strMiddle.charAt(n) != '/')
							isNumber = false;
					if(isNumber){
						nChunk++;
						if(nChunk == nChunkPlace){
							strTemplateBuffer[m] = (String)vDomain.get(k);
							break;						
						}
					}
				}
			}
		}
		sbTemp = new StringBuffer();
		int nBuffer = strTemplateBuffer.length;
		for(int k = 0; k < nBuffer; k++){
			if(k > 0)
				sbTemp.append(" ");
			sbTemp.append(strTemplateBuffer[k]);
		}
//
		strTemplateBuffer = null;
//
		return sbTemp;
	}

	/**	Given a Vector of String chunks, they are converted to TExtChunk objects.  The template
	 *		is used to to get the domains of the chunks.
	 *
	 * v. change to chunks
	 */
	protected Vector getOriginalChunks(TExtChunk template, Vector vAlChunkString){
		Vector vTemplateChunk = new Vector(2);
		Object oContent;
		Vector vAlChunkFinal = new Vector(5);
		String strChunkString;
		String strChunkDom;
		String strCompID;
		int nRunner = 0;

		for(Iterator it = template.iterator(); it.hasNext(); ){
			oContent = it.next();
			if(oContent instanceof TExtChunk)
				vTemplateChunk.add(((TExtChunk)oContent).domain());
		}
//
		oContent = null;
//
		for(Iterator it = vAlChunkString.iterator(); it.hasNext(); ){
			strChunkString = (String)it.next();
			strChunkDom = (String)it.next();
			TExtChunk newChunk = new TExtChunk();
			strCompID = (String)it.next();
			if(strChunkString.compareTo(strChunkDom) == 0)
				vAlChunkFinal.add(new TExtChunk(strChunkDom + "->" + (String)vTemplateChunk.get(nRunner)));
			else
				vAlChunkFinal.add(new TExtChunk(strChunkString, (String)vTemplateChunk.get(nRunner), strCompID));
			
			nRunner++;
			if(nRunner == vTemplateChunk.size())
				nRunner = 0;
		}
		return vAlChunkFinal;
	}
	
	/**	This function gets the tokens of the chunks to be aligned and passes them to Unit
	 *		Alignment.
	 */
	protected Vector getAlignmentInChunks(Vector vSourceChunks1, Vector vTargetChunks1){
		Vector vSourceTokens = new Vector();
		Vector vTargetTokens = new Vector();
		String strDummy = null;
		Hashtable hTemplate;
		Vector vUpdatedSChunks = new Vector(vSourceChunks1.size());
		Vector vUpdatedTChunks = new Vector(vTargetChunks1.size());
		
		TExtAligner aligner;
		
		if(bSourceIsFilipino)
			aligner = new TExtAligner('e');
		else
			aligner = new TExtAligner('f');
			
		for(Iterator it1 = vSourceChunks1.iterator(); it1.hasNext(); ){
			TExtChunk cSource = (TExtChunk)it1.next();
			if(cSource.size() == 0)
				vSourceTokens.add(cSource.domain());
			else
				for(Iterator it2 = cSource.iterator(); it2.hasNext(); )
					vSourceTokens.add(it2.next().toString());
			vSourceTokens.add(strDummy);
		}
		
		for(Iterator it1 = vTargetChunks1.iterator(); it1.hasNext(); ){
			TExtChunk cTarget = (TExtChunk)it1.next();
			if(cTarget.size() == 0)
				vTargetTokens.add(cTarget.domain());
			else
				for(Iterator it2 = cTarget.iterator(); it2.hasNext(); )
					vTargetTokens.add(it2.next().toString());
			vTargetTokens.add(strDummy);
		}

		hTemplate = aligner.align(vSourceTokens, vTargetTokens, false);

		Vector vFinalTokens = aligner.getUdpatedTokens();
//
		aligner = null;
//
		vSourceTokens = (Vector)vFinalTokens.get(0);
		vTargetTokens = (Vector)vFinalTokens.get(1);
		
		int nRun = 0;
		Vector vTokens = new Vector();
		for(Iterator it1 = vSourceTokens.iterator(); it1.hasNext(); ){
			String strFromFinal = (String)it1.next();
			
			if(strFromFinal == null){
				TExtChunk cSource;
				if(vTokens.size() == 1 && vTokens.get(0).toString().startsWith("[") && vTokens.get(0).toString().endsWith("]")){
					cSource = new TExtChunk();
					vTokens.clear();
					cSource.setChunk(vTokens, ((TExtChunk)vSourceChunks1.get(nRun)).domain(), "none");
				}
				else
					cSource = tlu.adjustChunkCompound(new TExtChunk(tlu.toStringChunk(vTokens), ((TExtChunk)vSourceChunks1.get(nRun)).domain(), tlu.makeCompoundIdentifier(vTokens)));
				vTokens = new Vector();
				nRun++;
				vUpdatedSChunks.add(cSource);
			}
			else
				vTokens.add(strFromFinal);
		}

		nRun = 0;
		vTokens = new Vector();
		for(Iterator it1 = vTargetTokens.iterator(); it1.hasNext(); ){
			String strFromFinal = (String)it1.next();
			
			if(strFromFinal == null){
				TExtChunk cTarget;
				if(vTokens.size() == 1 && vTokens.get(0).toString().startsWith("[") && vTokens.get(0).toString().endsWith("]")){
					cTarget = new TExtChunk();
					vTokens.clear();
					cTarget.setChunk(vTokens, ((TExtChunk)vTargetChunks1.get(nRun)).domain(), "none");
				}
				else
					cTarget = tlu.adjustChunkCompound(new TExtChunk(tlu.toStringChunk(vTokens), ((TExtChunk)vTargetChunks1.get(nRun)).domain(), tlu.makeCompoundIdentifier(vTokens)));
				vTokens = new Vector();
				nRun++;
				vUpdatedTChunks.add(cTarget);
			}
			else
				vTokens.add(strFromFinal);
		}

//
		vSourceChunks1 = null;
		vTargetChunks1 = null;
//
		Vector vResults = new Vector();
		vResults.add(vUpdatedSChunks);
		vResults.add(vUpdatedTChunks);
		vResults.add(hTemplate);
		return vResults;
	}
	

	/**	This is the main function for Template Refinement.
	 *	This calls all the functions that processes the given and performs the refinement
	 *		process.
	 *
	 *  CHANGE THIS TO CHUNK
	 */
	private Vector deriveChunkTR(AlignedChunk alChunk, AlignedChunk alMatch){
		TExtChunk cSource = alChunk.getSource();
		TExtChunk cTarget = alChunk.getTarget();
		String strOrigMatchDomain = cSource.domain();
		
		TExtChunk cTempSTemp = alMatch.getSource(), cTempTTemp = alMatch.getTarget();
		Vector vMatchesS;
		Vector vMatchesT;
		Vector vResult;
		
		Vector vSal = new Vector();
		Vector vTal = new Vector();
		vSal.add(alMatch.getSource());
		vTal.add(alMatch.getTarget());
		Hashtable hAlignment = (Hashtable) ((Vector)getAlignmentInChunks( vSal , vTal )).get(2);
		
		TExtChunk oldS = new TExtChunk(cSource.toString(), cSource.getCompoundIdentifier());
		oldS.setDomain(strOrigMatchDomain);
		TExtChunk oldT = new TExtChunk(cTarget.toString(), cTarget.getCompoundIdentifier());
		oldT.setDomain(strOrigMatchDomain);
		
		oldS = tlu.adjustChunkCompound(oldS);
		oldT = tlu.adjustChunkCompound(oldT);
		
	 	Vector vTempTokens = ((Vector)cTempSTemp.clone()) ;

	 	TExtChunk cTempS = (TExtChunk)alMatch.getSource().clone();
	 	
	 	vTempTokens = (Vector)cTempTTemp.clone();
	 	
	 	TExtChunk cTempT = (TExtChunk)alMatch.getTarget().clone();
		cTempSTemp = null;
		cTempTTemp = null;
		vTempTokens = null;
		
		
		vResult = getSimilarTokens(cTempS, cSource);
		println("vResult similar tokens: "+vResult);
		
		vMatchesS = (Vector)vResult.clone();
//FOR COMMON WORDS		
		if(tlu.computeSpecificity(vMatchesS, true) == 0)
			return null;
		
//END FOR COMMON WORDS
		if(cTempS.size() == vMatchesS.size() || vMatchesS.size() == 0)
			return null;

		
		vResult = getSimilarTokens(cTempT, cTarget);

		vMatchesT = (Vector)vResult.clone();

//FOR COMMON WORDS		
		if(tlu.computeSpecificity(vMatchesT, false) == 0)
			return null;
//END FOR COMMON WORDS
		if(cTempT.size() == vMatchesT.size() || vMatchesT.size() == 0)
			return null;

		/* At this point, there must be at least one unit match for both
		 * source and target language parts of the aligned sentence and template pair.
		 */
//		System.out.println("Checkpoint 1");
				
		/* Checking to see whether the aligned sentence and the template pair share
		 * the same valid template pattern.
		 */
		Vector vConfirmS = checkPattern(cTempS, cSource, vMatchesS);
		Vector vConfirmT = checkPattern(cTempT, cTarget, vMatchesT);
		if(((vConfirmS.get(0)).toString()).compareTo("false") == 0)
			return null;

		if(((vConfirmT.get(0)).toString()).compareTo("false") == 0)
			return null;

		vConfirmS = null;
		vConfirmT = null;

		//FOR VALIDITY OF CONSTANTS		
		if(!tlu.isValidSimilarities((Vector)vMatchesS.clone(), (Vector)vMatchesT.clone(), hAlignment))
			return null;
		//END FOR VALIDITY OF CONSTANTS
			
		/* Constructing the template String and forming the corresponding
		 * TExtChunks along the way.
		 */
	
		int nRetNextChunk = Integer.parseInt(tlu.returnNextChunk());
		vResult = constructChunks(cTempS, vMatchesS, "none", nRetNextChunk);
		StringBuffer sbTempS1 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vSourceChunks1 = (Vector)vResult.clone();
		
		String newSCompID = new String("none");
		if(!(cSource.getCompoundIdentifier().equals("none"))){
			newSCompID = new String(cSource.getCompoundIdentifier());
			vResult = constructChunks(cSource, vMatchesS, newSCompID, nRetNextChunk);
			newSCompID = (String)vResult.get(vResult.size()-1);
			vResult.remove(vResult.size()-1);
		}
		else
			vResult = constructChunks(cSource, vMatchesS, newSCompID, nRetNextChunk);
		StringBuffer sbTempS2 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vSourceChunks2 = (Vector)vResult.clone();
		
		vResult = constructChunks(cTempT, vMatchesT, "none", nRetNextChunk);
		StringBuffer sbTempT1 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vTargetChunks1 = (Vector)vResult.clone();
		
		String newTCompID = new String("none");
		if(!(cTarget.getCompoundIdentifier().equals("none"))){
			newTCompID = new String(cTarget.getCompoundIdentifier());
			vResult = constructChunks(cTarget, vMatchesT, newTCompID, nRetNextChunk);
			newTCompID = (String)vResult.get(vResult.size()-1);
			vResult.remove(vResult.size()-1);
		}
		else
			vResult = constructChunks(cTarget, vMatchesT, newTCompID, nRetNextChunk);
		StringBuffer sbTempT2 = (StringBuffer)vResult.get(vResult.size()-1);
		vResult.remove(vResult.size()-1);
		Vector vTargetChunks2 = (Vector)vResult.clone();

		vResult = null;
		vMatchesS = null;
		vMatchesT = null;
//
		/* At this point, a template String and element TExtChunks must have
		 * been formed.
		 */
		
		Vector vFcnResults = getAlignmentInChunks((Vector)vSourceChunks2.clone(), (Vector)vTargetChunks2.clone());
		vSourceChunks2 = (Vector)vFcnResults.get(0);
		vTargetChunks2 = (Vector)vFcnResults.get(1);
		Hashtable hTemplate = (Hashtable)vFcnResults.get(2);
//
		vFcnResults = null;
//

		TExtChunkSplitter txtChunkSplit1, txtChunkSplit2;
		txtChunkSplit1 = new TExtChunkSplitter(bSourceIsFilipino, tlu);
		txtChunkSplit2 = new TExtChunkSplitter(bSourceIsFilipino, tlu);
		Vector vChosen1 = null;
		Vector vChosen2 = null;
		Vector vTempS1 = null;
		Vector vTempS2 = null;
		Vector vTempT1 = null;
		Vector vTempT2 = null;
		if(txtChunkSplit1.alignChunks(vSourceChunks1, vTargetChunks1, hAlignment, 1) &&
			txtChunkSplit2.alignChunks(vSourceChunks2, vTargetChunks2, hTemplate, 2)){
			
//			System.out.println("BOTH WITH FINAL RESULTS");
			Vector vPossRes1 = txtChunkSplit1.getFinalResult();
			Vector vPossRes2 = txtChunkSplit2.getFinalResult();
			Vector vChosenRes = tlu.chooseAmongAlSplit(vPossRes1, vPossRes2);
			
			if(vChosenRes != null){
//				System.out.println("WITH CHOSEN RESULT");
				vChosen1 = (Vector)vChosenRes.get(0);
				vChosen2 = (Vector)vChosenRes.get(1);
//
				vPossRes1 = null;
				vPossRes2 = null;
				vChosenRes = null;
//				
				
				vSourceChunks1 = (Vector)vChosen1.get(1);
				vTargetChunks1 = (Vector)vChosen1.get(2);
				vTempS1 = (Vector)vChosen1.get(3);
				vTempT1 = (Vector)vChosen1.get(4);
				Vector vDummy1 = tlu.adjustChunkDiv(sbTempS1.toString(), (Vector)vSourceChunks1.clone(), "none");
				Vector vDummy2 = tlu.adjustChunkDiv(sbTempT1.toString(), (Vector)vTargetChunks1.clone(), "none");

				if(vDummy1 == null || vDummy2 == null)
					return null;

				sbTempS1 = (StringBuffer)vDummy1.remove(0);
				sbTempT1 = (StringBuffer)vDummy2.remove(0);
				
				vSourceChunks2 = (Vector)vChosen2.get(1);
				vTargetChunks2 = (Vector)vChosen2.get(2);
				vTempS2 = (Vector)vChosen2.get(3);
				vTempT2 = (Vector)vChosen2.get(4);
				vDummy1 = tlu.adjustChunkDiv(sbTempS2.toString(), (Vector)vSourceChunks2.clone(), newSCompID);
				vDummy2 = tlu.adjustChunkDiv(sbTempT2.toString(), (Vector)vTargetChunks2.clone(), newTCompID);

				if(vDummy1 == null || vDummy2 == null)
					return null;

				if(vDummy1.size() == 2){
					sbTempS2 = (StringBuffer)vDummy1.remove(1);
					newSCompID = new String((String)vDummy1.remove(0));
				}
				else
					sbTempS2 = (StringBuffer)vDummy1.remove(0);
					
				if(vDummy2.size() == 2){
					sbTempT2 = (StringBuffer)vDummy2.remove(1);
					newTCompID = new String((String)vDummy2.remove(0));
				}
				else
					sbTempT2 = (StringBuffer)vDummy2.remove(0);
					
				vDummy1 = null;
				vDummy2 = null;
			}
			else
				return null;
		}
		else
			return null;
//
		txtChunkSplit1 = null;
		txtChunkSplit2 = null;
//		
		Vector vAlChunkSString = new Vector(2);
		Vector vAlChunkTString = new Vector(2);
		
		vAlChunkSString.addAll(toStringVector(vSourceChunks1));
		vAlChunkSString.addAll(toStringVector(vSourceChunks2));
		vAlChunkTString.addAll(toStringVector(vTargetChunks1));
		vAlChunkTString.addAll(toStringVector(vTargetChunks2));

		Vector vAlChunkS1 = new Vector(vSourceChunks1.size()), vAlChunkT1 = new Vector(vTargetChunks1.size());
		Vector vAlChunkS2 = new Vector(vSourceChunks2.size()), vAlChunkT2 = new Vector(vTargetChunks2.size());

		for(Iterator it = vSourceChunks1.iterator(); it.hasNext(); ){
			TExtChunk outer = (TExtChunk)it.next();
			for(int k = 0; k < vTempS1.size(); k++){
				TExtChunk inner = (TExtChunk)vTempS1.get(k);
				if(outer.equals(inner)){
					vAlChunkS1.add(inner);
					TExtChunk cAlTarget = (TExtChunk)vTempT1.get(k);
					cAlTarget.setDomain(inner.domain());
					vAlChunkT1.add(cAlTarget);
					vTempS1.remove(k);
					vTempT1.remove(k);
					break;
				}
			}
		}

		for(Iterator it = vSourceChunks2.iterator(); it.hasNext(); ){
			TExtChunk outer = (TExtChunk)it.next();
			for(int k = 0; k < vTempS2.size(); k++){
				TExtChunk inner = (TExtChunk)vTempS2.get(k);
				if(outer.equals(inner)){
					vAlChunkS2.add(inner);
					TExtChunk cAlTarget = (TExtChunk)vTempT2.get(k);
					cAlTarget.setDomain(inner.domain());
					vAlChunkT2.add(cAlTarget);
					vTempS2.remove(k);
					vTempT2.remove(k);
					break;
				}
			}
		}

		Vector nSourceDomain = arrangeOrder(vSourceChunks2, vAlChunkS2, true);
		Vector nTargetDomain = arrangeOrder(vTargetChunks2, vAlChunkT2, true);
		
		Vector nSourceDomainTemp = arrangeOrder(vSourceChunks2, vAlChunkS2, false);
		
//		System.out.println("Checkpoint 4");
		
		sbTempS1 = adjustTemplateChunks(sbTempS1);
		sbTempS2 = adjustTemplateChunks(sbTempS2);
		sbTempT1 = adjustTemplateChunks(sbTempT1);
		sbTempT2 = adjustTemplateChunks(sbTempT2);
		
		Vector vDomainResult = adjustDomain((Vector)vAlChunkS2.clone(), (Vector)vAlChunkT2.clone());
		
		vAlChunkS2 = (Vector)vDomainResult.get(0);
		vAlChunkT2 = (Vector)vDomainResult.get(1);
		Vector vSourceDomain = (Vector)vDomainResult.get(2);
		Vector vTargetDomain = (Vector)vDomainResult.get(3);

//
		vDomainResult = null;
//

		sbTempS2 = mapDomain(vSourceDomain, sbTempS2, nSourceDomain);
		sbTempT2 = mapDomain(vTargetDomain, sbTempT2, nTargetDomain);

		Vector vReArrangeS = (Vector)vAlChunkS2.clone();
		Vector vReArrangeT = (Vector)vAlChunkT2.clone();

		for(int i = 0; i < nSourceDomainTemp.size(); i++){
			int nPosition = ((Integer)nSourceDomainTemp.get(i)).intValue();
			TExtChunk nNewS = (TExtChunk)vReArrangeS.get(i);
			vAlChunkS2.setElementAt(nNewS, nPosition);
			TExtChunk nNewT = (TExtChunk)vReArrangeT.get(i);
			vAlChunkT2.setElementAt(nNewT, nPosition);
		}

//
	vReArrangeS = null;
	vReArrangeT = null;
//
		
		int nSize = vAlChunkS2.size();
		TExtChunk tempTemp;
		TExtChunk tempInput;
		for(int k = 0; k < nSize; k++){
			tempTemp = (TExtChunk)vAlChunkS2.get(k);
			tempInput = (TExtChunk)vAlChunkS1.get(k);
			tempInput.setDomain(tempTemp.domain());
			vAlChunkS1.setElementAt(tempInput, k);

			tempTemp = (TExtChunk)vAlChunkT2.get(k);
			tempInput = (TExtChunk)vAlChunkT1.get(k);
			tempInput.setDomain(tempTemp.domain());
			vAlChunkT1.setElementAt(tempInput, k);
		}

		StringBuffer sbTemplateS, sbTemplateT;

		sbTemplateS = constructTemplate(sbTempS2.toString(), vAlChunkS2);
		
		sbTemplateT = constructTemplate(sbTempT2.toString(), vAlChunkT2);
		
		TExtChunk tSource2 = new TExtChunk(sbTemplateS.toString(), newSCompID);
		tSource2.setDomain(strOrigMatchDomain);
		TExtChunk tTarget2 = new TExtChunk(sbTemplateT.toString(), newTCompID);
		tTarget2.setDomain(strOrigMatchDomain);
		tSource2 = tlu.adjustChunkCompound(tSource2);
		tTarget2 = tlu.adjustChunkCompound(tTarget2);

	/*	JOptionPane.showMessageDialog(null, "TR");
		JOptionPane.showMessageDialog(null, "tSource2-->" + tSource2);
		JOptionPane.showMessageDialog(null, "tTarget2-->" + tTarget2);
	*/	
		Vector vAlChunkSRef = getOriginalChunks(tSource2, vAlChunkSString);
		Vector vAlChunkTRef = getOriginalChunks(tTarget2, vAlChunkTString);
		
		int nRefS = vAlChunkSRef.size();
		for(int i = nRefS/2; i < nRefS; i++){
			TExtChunk ref = (TExtChunk)vAlChunkSRef.get(i);
			TExtChunk changeS = (TExtChunk)vAlChunkS2.get(i-vAlChunkSRef.size()/2);
			if(changeS.size() == 0){
				changeS.setDomain(ref.domain());
				vAlChunkS2.setElementAt(changeS, i-vAlChunkSRef.size()/2);
			}

			TExtChunk changeT = (TExtChunk)vAlChunkT2.get(i-vAlChunkSRef.size()/2);
			if(changeT.size() == 0){
				if(ref.domain().contains("->"))
					changeT.setDomain(ref.domain());
				else{
					int nRefT = vAlChunkTRef.size();
					for(int j = nRefT/2; j < nRefT; j++){
						TExtChunk targetRef = (TExtChunk)vAlChunkTRef.get(j);
						if(targetRef.size() == 0 && targetRef.domain().contains("->"+ref.domain()))
							changeT.setDomain(targetRef.domain());		
					}
				}
				vAlChunkT2.setElementAt(changeT, i-vAlChunkSRef.size()/2);
			}
		}
		
		Vector vAlChunkSFinal = new Vector(2);
		Vector vAlChunkTFinal = new Vector(2);
		vAlChunkSFinal.addAll(vAlChunkS1);
		vAlChunkSFinal.addAll(vAlChunkS2);
		vAlChunkTFinal.addAll(vAlChunkT1);
		vAlChunkTFinal.addAll(vAlChunkT2);
//
		vAlChunkS1 = null;
		vAlChunkS2 = null;
		vAlChunkT1 = null;
		vAlChunkT2 = null;
//
	
		for(int i = 0; i < vAlChunkSFinal.size(); i++){
			TExtChunk cChunkS = (TExtChunk)vAlChunkSFinal.get(i);
			String strDomain = cChunkS.domain();
			if(strDomain.contains("->")){
				String[] oldNew = strDomain.split("->");
				if(oldNew[0].compareTo(oldNew[1]) == 0){
					cChunkS.setDomain(oldNew[0]);
					vAlChunkSFinal.setElementAt(cChunkS, i);
				}
			}
		}

		for(int i = 0; i < vAlChunkTFinal.size(); i++){
			TExtChunk cChunkT = (TExtChunk)vAlChunkTFinal.get(i);
			String strDomain = cChunkT.domain();
			if(strDomain.contains("->")){
				String[] oldNew = strDomain.split("->");
				if(oldNew[0].compareTo(oldNew[1]) == 0){
					cChunkT.setDomain(oldNew[0]);
					vAlChunkTFinal.setElementAt(cChunkT, i);
				}
			}
		}
		
		/* At this point, TExtTemplates and TExtChunks must have already been formulated.
		 */
		Vector vNewRefinement = new Vector(7);
		AlignedChunk alNewTemplate = new AlignedChunk(tSource2, tTarget2);
		AlignedChunk alOldTemplate = new AlignedChunk(oldS, oldT);
		vNewRefinement.add(alNewTemplate);
		vNewRefinement.add(alOldTemplate);
		vNewRefinement.add(vAlChunkSFinal.clone());
		vNewRefinement.add(vAlChunkTFinal.clone());
		vNewRefinement.add(new Boolean(!bSourceIsFilipino));
		vNewRefinement.add(tlu.combineHash(hAlignment, hTemplate));
		vNewRefinement.add(new Boolean(true));
		
		System.out.println("1541: vNewRefinement "+vNewRefinement);
		
		return vNewRefinement;
	}		

//MIN START - updateDBWithRefinement
	/**
	 * Updates the database by: inserting the new, refined TExtTemplates in
	 *	vSourceRefinements and vTargetRefinements into and removing the old 
	 *	TExtTemplate objects in vOldSourceBases from the Templates Database;
	 *	and, inserting the new TExtChunks in vChunkAlignments into the Chunks
	 *	Database.
	 * This works in tandem with TExtLearner.refineTemplate(AS,V) and
	 *	especially with TExtLearner[Kat].checkForRefinement(T,T,V,V)
	 *
	 * @param Vector vSourceRefinements vector of refined TExtTemplate objects
	 *	in the source language
	 * @param Vector vTargetRefinements vector of refined TExtTemplate objects
	 *	in the target language
	 * @param Vector vOldSourceBases vector of old TExtTemplate objects to
	 *	remove from the Templates Database
	 * @param Vector vChunkAlignments vector of new TExtChunk objects to store
	 *	into the Chunks Database
	 * @param boolean bSourceIsEnglish flag indicating whether the source
	 *	objects are in English
	 *
	 * @see TExtLearner.updateDatabaseWithRefinement(V,V,V,V,b)
	 *
	 * @note Check the comments in TExtLearner.refineTemplate(AS,V) just
	 *	after Checkpoint D.
	 *
	 *	not tested
	 */
	public void updateDB(AlignedChunk atRefinement,
		AlignedChunk atOldTemplate,
		Vector vSourceChunks,
		Vector vTargetChunks,
		boolean bSourceIsEnglish,
		Hashtable hAlignedWords,
		boolean bIsFromRefineTemplate) {

		System.out.println("updateDB");
		vTRContent.add(atOldTemplate);
		
		TExtChunk tSourceRefinement = (TExtChunk)atRefinement.getSource();
		TExtChunk tTargetRefinement = (TExtChunk)atRefinement.getTarget();
		TExtChunk tOldSrcTemplate = null;
		TExtChunk tOldTargetTemplate = null;
		String strSource, strTarget, strSComp, strTComp;
		boolean bCheck = true,
			bExactDuplicate = false;
		ResultSet rsDuplicate = null;
		AlignedChunk atNewRefinement = null;
				
		DBConnector.setAutoCommit(false);
		
		if(bSourceIsEnglish) {
			strSource = "English";
			strTarget = "Filipino";
		} else {
			strSource = "Filipino";
			strTarget = "English";			
		}

		//check for duplicate templates with different vars
		//if there is a duplicate -> ignore all processes...
		//checks: for an existing refined template in the DB
		
		String strDuplicate = "SELECT chunkID FROM Chunks WHERE "
			+ "chunk" + strSource + " = '" 
			+ tSourceRefinement.toString().replace("'","''") + "'"
			+ " AND chunk" + strTarget + " = '" 
			+ tTargetRefinement.toString().replace("'","''") + "'"
			+ " AND compID" + strSource + " = '" 
			+ tSourceRefinement.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '" 
			+ tTargetRefinement.getCompoundIdentifier() + "'";
		
		rsDuplicate = DBConnector.select(strDuplicate);
		
		try {
			if(rsDuplicate.next()) {
				bExactDuplicate = true;
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();

			System.out.println("There was an error while inserting the refined" 
			+" templates in the database.");

			bCheck = false;
		}

		if(!bExactDuplicate) {
			atNewRefinement = checkForDuplicateTempWithDiffVar(tSourceRefinement,
										tTargetRefinement, strSource, strTarget);
										
			if(atNewRefinement != null) {
				tSourceRefinement = atNewRefinement.getSource();
				tTargetRefinement = atNewRefinement.getTarget();
			}
		}

		if(bCheck) {
			if(bIsFromRefineTemplate) {
				tOldSrcTemplate = (TExtChunk)atOldTemplate.getSource();
				tOldTargetTemplate = (TExtChunk)atOldTemplate.getTarget();
		
//				System.out.println("**************************************************");
//				System.out.println("Delete the old template from the database.");
		
				bCheck = DBConnector.update("DELETE FROM Chunks WHERE "
					+ "chunk" + strSource + " = '" 
					+ tOldSrcTemplate.toString().replace("'","''") + "'"
					+ " AND chunk" + strTarget + " = '" 
					+ tOldTargetTemplate.toString().replace("'","''") + "'"
					+ " AND compID" + strSource + " = '" 
					+ tOldSrcTemplate.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '" 
					+ tOldTargetTemplate.getCompoundIdentifier() + "'");
		
				if(!bCheck)
					System.out.println("There is no such template to be remove, or, "
						+ "such a template has already been removed.");
				else
					System.out.println("Template successfully removed.");
			}
	
//			System.out.println("**************************************************");
//			System.out.println("Update the template database with the refinements.");

			if(atNewRefinement == null || bExactDuplicate) {
				int nCnstCntS = 0, nCnstCntT = 0;
				
				for(Iterator it = tSourceRefinement.iterator(); it.hasNext(); ) {
					Object o = it.next();
					if(o instanceof String){
//						System.out.println(o);
						nCnstCntS++;
					}
					o = null;
				}
				for(Iterator it = tTargetRefinement.iterator(); it.hasNext(); ) {
					Object o = it.next();
					if(o instanceof String){
//						System.out.println(o);
						nCnstCntT++;
					}
					o = null;
				}
				
				// insert the templates
				String strUpdate = "INSERT INTO Chunks"
					+ " (chunk" + strSource + ", chunk" + strTarget 
					+ ", compID" + strSource + ", compID" + strTarget
					+ ", constCnt" + strSource.substring(0,3) 
					+ ", constCnt" + strTarget.substring(0,3)
					+ ", domain"
					+ ") VALUES ('" 
					+ tSourceRefinement.toString().replace("'","''") + "', '" 
					+ tTargetRefinement.toString().replace("'","''") + "', '" 
					+ tSourceRefinement.getCompoundIdentifier() + "', '" 
					+ tTargetRefinement.getCompoundIdentifier() + "', '"
					+ nCnstCntS + "', '"
					+ nCnstCntT + "', '" 
					+ tSourceRefinement.domain() +"')";
	
				
				bCheck = DBConnector.update(strUpdate);
				
				
				if(vTRTemplates.size() > 0) {
					vTRTemplates.removeElementAt(vTRTemplates.size()-1);
				}
				vTRTemplates.add(atNewRefinement);
			}
		}

		if(!bCheck)
			System.out.println("There was an error while adding the refined templates.");
		else
			System.out.println("Refined templates successfully added.");
			
		if(bCheck) {
	
			/**
			 *	for adding chunks in the DB
			 *
			 *	assumptions:
			 *		- the given chunks are aligned (meaning there is an equal number of
			 *		chunks in the source and in the target vector of chunks)
			 *		- there is always a whole number in the domain of a given chunk
			 *		- for each given chunk in the vector of source and target,
			 *		the source and target domain is equal
			 *		- domains have square brackets and slashes
			 */

	 		if(vSourceChunks.size() == vTargetChunks.size()) {
	 			//assumes: cSource.domain() == cTarget.domain()
	 			TExtChunk cSource = null;
	 			TExtChunk cTarget = null;
	 			String strSrcDomain = null;
	 			String strTarDomain = null;
	 			
		 		for(int i = 0; i < vSourceChunks.size(); i++) {
					//chunks from Source and Target
		 			cSource = (TExtChunk) vSourceChunks.get(i);
		 			cTarget = (TExtChunk) vTargetChunks.get(i);
		 			
		 			strSrcDomain = cSource.domain();
		 			strTarDomain = cTarget.domain();
	
		 			/**
		 			 *	if either of the given chunk (source/target) is domain only
		 			 *	
		 			 *	assumptions:
		 			 *		- if the chunk contains only a domain and -> exists 
		 			 *		then the chunk is to be added
		 			 *		- else it is just ignored
		 			 */
		 			if(cSource.size() <= 0 || cTarget.size() <= 0) {
//		 				System.out.println("<---DOMAIN ONLY-->");
//		 				System.out.println("Source domain: "+strSrcDomain);
//		 				System.out.println("Target domain: "+strTarDomain);
		 				
		 				String[] arrStrSrcDomains = null;
		 				String[] arrStrTarDomains = null;
		 				String strSNewDom = null;
		 				String strTNewDom = null;
		 				
		 				if(strSrcDomain.contains("->") && strTarDomain.contains("->")){
//			 				System.out.println("<---ARROW1-->");
		 					arrStrSrcDomains = strSrcDomain.split("->");
							arrStrTarDomains = strTarDomain.split("->");
							strSNewDom = checkDomain(arrStrSrcDomains[1],
								vSourceChunks, vTargetChunks, cSource, cTarget);
							strTNewDom = checkDomain(arrStrTarDomains[1],
								vSourceChunks, vTargetChunks, cSource, cTarget);

		 					//check if there's a similar X.Y in the dom
		 					//ex. [1.1]->[2/1.1]; [1.1]->[4]
		 					//if there is then no need to add it
		 					if(!isChunkADomAndInDomain(arrStrSrcDomains[0],
		 						arrStrSrcDomains[1]) &&
		 						!isChunkADomAndInDomain(arrStrTarDomains[0],
		 						arrStrTarDomains[1])) {
								//check if checkDomain changed in the whole number part
								//&& if X.Y is already in the DB: assume: X.Y already exists
				 				if(getWholeNoDomain(arrStrSrcDomains[1]) == null &&
				 					!arrStrSrcDomains[1].substring(0,
				 					arrStrSrcDomains[1].indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
				 					strSNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrSrcDomains[1],
				 						strSNewDom, strSource, strTarget, true);
								}
								
								//check if checkDomain changed in the whole number part
				 				if(getWholeNoDomain(arrStrTarDomains[1]) == null &&
				 					!arrStrTarDomains[1].substring(0,
				 					arrStrTarDomains[1].indexOf(".")).equalsIgnoreCase(strTNewDom.substring(0,
				 					strTNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tTargetRefinement = modOrigin(tSourceRefinement, 
				 						tTargetRefinement, arrStrTarDomains[1],
				 						strTNewDom, strSource, strTarget, false);
								}
				 				
			 					addChunkToDB(new TExtChunk(arrStrSrcDomains[0],
	 								strSNewDom, cSource.getCompoundIdentifier()),
	 								new TExtChunk(arrStrTarDomains[0],
	 								strTNewDom, cTarget.getCompoundIdentifier()),
	 								strSource, strTarget, tSourceRefinement,
	 								tTargetRefinement, vSourceChunks, vTargetChunks);
		 					}
		 				}else if(strSrcDomain.contains("->")) {
//			 				System.out.println("<---ARROW2-->");
							arrStrSrcDomains = strSrcDomain.split("->");
							strSNewDom = checkDomain(arrStrSrcDomains[1],
		 						vSourceChunks, vTargetChunks, cSource, cTarget);
		 						
		 					//check if there's a similar X.Y in the dom
		 					//ex. [1.1]->[2/1.1]
		 					//if there is then no need to add it
		 					if(!isChunkADomAndInDomain(arrStrSrcDomains[0],
		 						arrStrSrcDomains[1])) {

								//check if checkDomain changed in the whole number part
				 				if(getWholeNoDomain(arrStrSrcDomains[1]) == null &&
				 					!arrStrSrcDomains[1].substring(0,
				 					arrStrSrcDomains[1].indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
				 					strSNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrSrcDomains[1],
				 						strSNewDom, strSource, strTarget, true);
									tTargetRefinement = modOrigin(tSourceRefinement,
										tTargetRefinement, arrStrSrcDomains[1],
										strSNewDom, strSource, strTarget, false);
								}
			 					
				 				addChunkToDB(new TExtChunk(arrStrSrcDomains[0],
	 								strSNewDom,
	 								cSource.getCompoundIdentifier()), 
	 								new TExtChunk(cTarget.toString(),
	 								strSNewDom,
	 								cTarget.getCompoundIdentifier()),
	 								strSource, strTarget,
	 								tSourceRefinement, tTargetRefinement,
	 								vSourceChunks, vTargetChunks);
 							}
						}else if(strTarDomain.contains("->")) {
//			 				System.out.println("<---ARROW3-->");
							arrStrTarDomains = strTarDomain.split("->");
							strTNewDom = checkDomain(arrStrTarDomains[1],
								vSourceChunks, vTargetChunks, cSource, cTarget);
								
		 					//check if there's a similar X.Y in the dom
		 					//ex. [1.1]->[2/1.1]
		 					//if there is then no need to add it
		 					if(!isChunkADomAndInDomain(arrStrTarDomains[0],
		 						arrStrTarDomains[1])) {
								//check if checkDomain changed in the whole number part

				 				if(getWholeNoDomain(arrStrTarDomains[1]) == null &&
				 					!arrStrTarDomains[1].substring(0,
				 					arrStrTarDomains[1].indexOf(".")).equalsIgnoreCase(strTNewDom.substring(0,
				 					strTNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrTarDomains[1],
				 						strTNewDom, strSource, strTarget, true);
				 					tTargetRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, arrStrTarDomains[1],
				 						strTNewDom, strSource, strTarget, false);
								}

								addChunkToDB(new TExtChunk(cSource.toString(),
	 								strTNewDom,
	 								cSource.getCompoundIdentifier()), 
	 								new TExtChunk(arrStrTarDomains[0],
	 								strTNewDom,
	 								cTarget.getCompoundIdentifier()),
	 								strSource, strTarget,
	 								tSourceRefinement, tTargetRefinement,
	 								vSourceChunks, vTargetChunks);
							}
						}
						
		 				arrStrSrcDomains = null;
		 				arrStrTarDomains = null;
		 				strSNewDom = null;
		 				strTNewDom = null;

					}else{
//		 				System.out.println("<---CHUNKS-->");
		 				
						//get all chunks + domain in the DB with domain same as the domain of the Chunk
			 			Vector vChunksDB = getChunksFrDB(strSrcDomain, strSource, strTarget);
//						System.out.println("vChunksDB: "+vChunksDB);
		 					
			 			if(vChunksDB != null) {
							//storage of source TExtChunks in the DB
				 			Vector vSrcChunksFrDB = (Vector) vChunksDB.get(0);
							//storage of target TExtChunks in the DB
				 			Vector vTarChunksFrDB = (Vector) vChunksDB.get(1);
							boolean bMatch = false;

							int nDBIndex = 0;
							
							TExtChunk cSrcChunksFrDB = null;
							TExtChunk cTarChunksFrDB = null;
							boolean bSChunkFrDBisNotXY;
							boolean bTChunkFrDBisNotXY;
							boolean bSIsMatch;
							boolean bTIsMatch;

							//while all chunksDB && no match
							while(nDBIndex < vSrcChunksFrDB.size() && !bMatch) {
								//does the chunks in both source and target 
								//match to the chunks in the DB
								cSrcChunksFrDB = (TExtChunk)vSrcChunksFrDB.get(nDBIndex);
								cTarChunksFrDB = (TExtChunk)vTarChunksFrDB.get(nDBIndex);
								bSChunkFrDBisNotXY = chunkFrDBisNotXY(cSrcChunksFrDB);
								bTChunkFrDBisNotXY = chunkFrDBisNotXY(cTarChunksFrDB);
								bSIsMatch = isMatch(cSrcChunksFrDB, cSource);
								bTIsMatch = isMatch(cTarChunksFrDB, cTarget);
								
								if(bSChunkFrDBisNotXY &&
									bTChunkFrDBisNotXY &&
									bSIsMatch && 
									bTIsMatch) {

//									System.out.println("-->IS MATCH<--");
									//assign the domain of the chunks (S&T) based on the chunks in the DB
									Vector vAOriginAndChunksWithDomains = findChunkDomains((TExtChunk)vSrcChunksFrDB.get(nDBIndex),
												(TExtChunk)vTarChunksFrDB.get(nDBIndex), cSource, cTarget, strSource,
												strTarget, hAlignedWords, vSourceChunks, vTargetChunks);
									
									if(vAOriginAndChunksWithDomains != null) {
//							 			System.out.println("-->FOUND DOMAIN<--");
										bMatch = true;
										//this calls addChunkToDB()
										bCheck = addAlignedChunksToDB(vAOriginAndChunksWithDomains, strSource, strTarget,
													vSourceChunks, vTargetChunks);
									}//else: if vAChunksWithDomains or there are no subchunks to be added
									
									vAOriginAndChunksWithDomains = null;
								}
								nDBIndex++;
							}
							//no match means new domain
							if(!bMatch){
//				 				System.out.println("NEW DOMAIN! - no match");
			 					String strSNewDom = checkDomain(strSrcDomain,
										vSourceChunks, vTargetChunks, cSource, cTarget);
								//--->>> error#2
								//ensure that there's a chunk learned before adding a domain in the template										
								//check if checkDomain changed in the whole number part
				 				if(getWholeNoDomain(strSrcDomain) == null &&
				 					!strSrcDomain.substring(0,
				 					strSrcDomain.indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
				 					strSNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					tSourceRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, strSrcDomain, strSNewDom,
				 						strSource, strTarget, true);
				 					tTargetRefinement = modOrigin(tSourceRefinement,
				 						tTargetRefinement, strSrcDomain, strSNewDom,
				 						strSource, strTarget, false);
								}

								bCheck = addChunkToDB(new TExtChunk(cSource.toString(),
											strSNewDom,
											cSource.getCompoundIdentifier()),
											new TExtChunk(cTarget.toString(),
											strSNewDom,
											cTarget.getCompoundIdentifier()),
											strSource, strTarget,
											tSourceRefinement, tTargetRefinement,
											vSourceChunks, vTargetChunks);
					 			//System.out.println("NEW DOMAIN ADDED END - no match");
					 			strSNewDom = null;
							}
							
							vSrcChunksFrDB = null;
							vTarChunksFrDB = null;
							
							cSrcChunksFrDB = null;
							cTarChunksFrDB = null;							
						}
						//else: chunksDB == null
						else {
		 					String strSNewDom = checkDomain(strSrcDomain,
									vSourceChunks, vTargetChunks, cSource, cTarget);
									
							//check if checkDomain changed in the whole number part
			 				if(getWholeNoDomain(strSrcDomain) == null &&
			 					!strSrcDomain.substring(0,
			 					strSrcDomain.indexOf(".")).equalsIgnoreCase(strSNewDom.substring(0,
			 					strSNewDom.indexOf(".")))) {
								//if it did then change the template or the chunk
			 					tSourceRefinement = modOrigin(tSourceRefinement,
			 						tTargetRefinement, strSrcDomain, strSNewDom,
			 						strSource, strTarget, true);
			 					tTargetRefinement = modOrigin(tSourceRefinement,
			 						tTargetRefinement, strSrcDomain, strSNewDom,
			 						strSource, strTarget, false);
							}

							bCheck = addChunkToDB(new TExtChunk(cSource.toString(),
											strSNewDom,
											cSource.getCompoundIdentifier()),
											new TExtChunk(cTarget.toString(),
											strSNewDom,
											cTarget.getCompoundIdentifier()),
											strSource, strTarget,
											tSourceRefinement, tTargetRefinement,
											vSourceChunks, vTargetChunks);
					 		//System.out.println("NEW DOMAIN ADDED END - not in DB");
					 		strSNewDom = null;
						}
					}//end of else with if(cSource.size() <= 0 || cTarget.size() <= 0)

				}//end of for-loop
		 		
		 		//generalize parent domain here...
		 		//save the generalization to DB .. or call modifyparentdomain, domains are blank
		 		// pang linis lang to, pang alis ng domain na walang chunk
		 		/*
		 		 * generalization includes: removing duplicates in the parent : 7.1/7.1
		 		 * 
		 		 */
		 		
		 		
		 		modifyParentDomain(tSourceRefinement,tTargetRefinement,"","" );
		 		
	 			cSource = null;
	 			cTarget = null;
	 			strSrcDomain = null;
	 			strTarDomain = null;

				atNewRefinement = new AlignedChunk(tSourceRefinement, tTargetRefinement);
				if(vTRTemplates.size() > 0) {
					vTRTemplates.removeElementAt(vTRTemplates.size()-1);
				}
				vTRTemplates.add(atNewRefinement);

				//--------------->>>check if all doms of the templates are in the db
				//checkDBDomainExistence(tSourceRefinement, tTargetRefinement, strSource, strTarget);

	 		}else {
	 			System.out.println("Error! Source and Target Chunks are unequal.");
	 		}
		}
		
		if(!bCheck)
			System.out.println("There was an error while adding the chunks.");
		else
			System.out.println("Chunks successfully added.");

		//END
		
		if(!bCheck)
			DBConnector.rollback();
		else
			DBConnector.commit();
		
		DBConnector.setAutoCommit(true);

		//null objects
		tSourceRefinement = null;
		tTargetRefinement = null;
		tOldSrcTemplate = null;
		tOldTargetTemplate = null;
		
		strSource = null;
		strTarget = null;
		strSComp = null;
		strTComp = null;

		rsDuplicate = null;
	}
	
	
	
	
	
	/**
	 * 
	 * v. modified for chunks
	 * 
	 * @param tSourceRefinement
	 * @param tTargetRefinement
	 * @param strSource
	 * @param strTarget
	 * @return
	 */
	protected AlignedChunk checkForDuplicateTempWithDiffVar(TExtChunk tSourceRefinement,
		TExtChunk tTargetRefinement, String strSource, String strTarget) {
		
		println("tSourceRefinement size" + tSourceRefinement.size());
		tSourceRefinement.trimToSize();
		Object oTmp = null,
			oTmpRef = null;
		TExtChunk tTmp = null;
		Vector vSTemplatesFrDB = new Vector();
		Vector vTTemplatesFrDB = new Vector();
		StringBuffer strbSRegexp = new StringBuffer("^");
		StringBuffer strbTRegexp = new StringBuffer("^");
		ResultSet rsDomain = null;

		//check if the templates have whole no vars
		for(int i = 0; i < tSourceRefinement.size(); i++) {
			oTmp = tSourceRefinement.get(i);
			if(oTmp instanceof TExtChunk) {
				strbSRegexp.append("\\\\[([0-9]+(\\\\.[0-9]+)?/?)+\\\\]");
			}else {
				strbSRegexp.append(oTmp);
			}
			strbSRegexp.append(" ");
		}
		
		for(int i = 0; i < tTargetRefinement.size(); i++) {
			oTmp = tTargetRefinement.get(i);
			if(oTmp instanceof TExtChunk) {
				strbTRegexp.append("\\\\[([0-9]+(\\\\.[0-9]+)?/?)+\\\\]");
			}else {
				strbTRegexp.append(oTmp);
			}
			strbTRegexp.append(" ");
		}

			//select all in the db with same structure
			rsDomain = DBConnector.select("SELECT chunk"+
			strSource + " , chunk" + strTarget + " , compID" + strSource +
			" , compID" + strTarget + 
			" FROM Chunks"+
			" WHERE chunk"+strSource+
			" REGEXP '"+strbSRegexp.toString().trim().replace("'","''")+"$"+
			"' AND chunk"+strTarget+
			" REGEXP '"+strbTRegexp.toString().trim().replace("'","''")+"$"+
			"' AND compID"+strSource+" = '"+tSourceRefinement.getCompoundIdentifier()+
			"' AND compID"+strTarget+" = '"+tTargetRefinement.getCompoundIdentifier()+"'");
			
			String sQuery = "SELECT chunk"+
			strSource + " , chunk" + strTarget + " , compID" + strSource +
			" , compID" + strTarget + 
			" FROM Chunks"+
			" WHERE chunk"+strSource+
			" REGEXP '"+strbSRegexp.toString().trim().replace("'","''")+"$"+
			"' AND chunk"+strTarget+
			" REGEXP '"+strbTRegexp.toString().trim().replace("'","''")+"$"+
			"' AND compID"+strSource+" = '"+tSourceRefinement.getCompoundIdentifier()+
			"' AND compID"+strTarget+" = '"+tTargetRefinement.getCompoundIdentifier()+"'";
	
			try {
				while(rsDomain.next()){
					
					String strSrc = rsDomain.getString("chunk" + strSource);
					String strTar = rsDomain.getString("chunk" + strTarget);
					String strCompIDS = rsDomain.getString("compID" + strSource);
					String strCompIDT = rsDomain.getString("compID" + strTarget);
					vSTemplatesFrDB.add(tlu.adjustChunkCompound(new TExtChunk(strSrc, strCompIDS)));
					vTTemplatesFrDB.add(tlu.adjustChunkCompound(new TExtChunk(strTar, strCompIDT)));
					
//					System.out.println("FOUND A DOMAIN!");
					
				}
			} catch(SQLException sqle){
				System.out.println("There is an SQL Error!");
				sqle.printStackTrace();
			}
			
			AlignedChunk atNewRefinement = null;
			TExtChunk tNewSource = null;
			TExtChunk tNewTarget = null; 
			StringBuffer strbNewSource = new StringBuffer();
			StringBuffer strbNewTarget = new StringBuffer();
			StringBuffer strbDBSource = new StringBuffer();
			StringBuffer strbDBTarget = new StringBuffer();
			String[] arrStrDBVar;
			String[] arrStrRef;
			Vector vFinVar = new Vector(1);
			boolean bRefIsInDB;

			//check the selected db if there is already a template
			//that have whole no vars
			for(int i = 0; i < vSTemplatesFrDB.size(); i++) {
				tTmp = (TExtChunk)vSTemplatesFrDB.get(i);
				for(int j = 0; j < tTmp.size(); j++) {
					oTmp = tTmp.get(j);
					if(oTmp instanceof TExtChunk) {
						oTmpRef = (TExtChunk)tSourceRefinement.get(j);
						
						//check if var in tSourceRef is already in oTmp whole no: no need
						strbNewSource.append("[");
						arrStrDBVar = stripSquareBrackets(oTmp.toString()).split("/");
						arrStrRef = stripSquareBrackets(oTmpRef.toString()).split("/");
						
						for(int k = 0; k < arrStrDBVar.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrRef.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrDBVar[k]) && 
									!isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef) &&
									(arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l])
									|| (arrStrRef[l].contains(".") && 
									arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l].substring(0,
									arrStrRef[l].indexOf(".")))))) {
									strbNewSource.append(arrStrDBVar[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewSource.append("/");
									}
								}else if(!vFinVar.contains(arrStrRef[l]) && 
									!isWholePresent(arrStrRef[l], arrStrDBVar, arrStrRef) &&
									arrStrDBVar[k].contains(".") && 
									arrStrRef[l].equalsIgnoreCase(arrStrDBVar[k].substring(0,
									arrStrDBVar[k].indexOf(".")))) {
									strbNewSource.append(arrStrRef[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[l]);
									if(k < arrStrDBVar.length) {
										strbNewSource.append("/");
									}
								}
							}
							
							if((!vFinVar.contains(arrStrDBVar[k])
								|| (arrStrDBVar[k].contains(".")
								&& !vFinVar.contains(arrStrDBVar[k].substring(0,
								arrStrDBVar[k].indexOf(".")))))
								&& !isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								if (!vFinVar.contains(arrStrDBVar[k])){
									strbNewSource.append(arrStrDBVar[k]);
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewSource.append("/");
									}									
									
								}

								
							}

						}
						
						for(int k = 0; k < arrStrRef.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrDBVar.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrRef[k]) && 
									!isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef) &&
									(arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l])
									|| (arrStrDBVar[l].contains(".") && 
									arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l].substring(0,
									arrStrDBVar[l].indexOf(".")))))) {
									strbNewSource.append(arrStrRef[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewSource.append("/");
									}
								}else if(!vFinVar.contains(arrStrDBVar[l]) && 
									!isWholePresent(arrStrDBVar[l], arrStrDBVar, arrStrRef) &&
									arrStrRef[k].contains(".") && 
									arrStrDBVar[l].equalsIgnoreCase(arrStrRef[k].substring(0,
									arrStrRef[k].indexOf(".")))) {
									strbNewSource.append(arrStrDBVar[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[l]);
									if(k < arrStrRef.length) {
										strbNewSource.append("/");
									}
								}
							}

							if((!vFinVar.contains(arrStrRef[k])
								|| (arrStrRef[k].contains(".")
								&& !vFinVar.contains(arrStrRef[k].substring(0,
								arrStrRef[k].indexOf(".")))))
								&& !isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								
								if(!vFinVar.contains(arrStrRef[k])){
									strbNewSource.append(arrStrRef[k]);
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewSource.append("/");
									}
									
								}

							}
						}
						
						while(strbNewSource.toString().endsWith("/")) {
							strbNewSource.deleteCharAt(strbNewSource.length()-1);
						}
												
						strbNewSource.append("]");
						
					}else {
						strbNewSource.append(oTmp.toString());
					}

					strbDBSource.append(oTmp.toString());
					strbDBSource.append(" ");
					strbNewSource.append(" ");
				}
			}
			
			//reset vFinVar
			vFinVar = new Vector(1);

			//check the selected db if there is already a template
			//that have whole no vars
			for(int i = 0; i < vTTemplatesFrDB.size(); i++) {
				tTmp = (TExtChunk)vTTemplatesFrDB.get(i);
				for(int j = 0; j < tTmp.size(); j++) {
					oTmp = tTmp.get(j);
					if(oTmp instanceof TExtChunk) {
						oTmpRef = (TExtChunk)tTargetRefinement.get(j);
						//check if var in tTargetRef is already in oTmp whole no: no need
						strbNewTarget.append("[");
						arrStrDBVar = stripSquareBrackets(oTmp.toString()).split("/");
						arrStrRef = stripSquareBrackets(oTmpRef.toString()).split("/");
						
						for(int k = 0; k < arrStrDBVar.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrRef.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrDBVar[k]) && 
									!isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef) &&
									(arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l])
									|| (arrStrRef[l].contains(".") && 
									arrStrDBVar[k].equalsIgnoreCase(arrStrRef[l].substring(0,
									arrStrRef[l].indexOf(".")))))) {
									strbNewTarget.append(arrStrDBVar[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewTarget.append("/");
									}
								}else if(!vFinVar.contains(arrStrRef[l]) && 
									!isWholePresent(arrStrRef[l], arrStrDBVar, arrStrRef) &&
									arrStrDBVar[k].contains(".") && 
									arrStrRef[l].equalsIgnoreCase(arrStrDBVar[k].substring(0,
									arrStrDBVar[k].indexOf(".")))) {
									strbNewTarget.append(arrStrRef[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[l]);
									if(k < arrStrDBVar.length) {
										strbNewTarget.append("/");
									}
								}
							}
							
							if((!vFinVar.contains(arrStrDBVar[k])
								|| (arrStrDBVar[k].contains(".")
								&& !vFinVar.contains(arrStrDBVar[k].substring(0,
								arrStrDBVar[k].indexOf(".")))))
								&& !isWholePresent(arrStrDBVar[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								
								if(!vFinVar.contains(arrStrDBVar[k])){
									strbNewTarget.append(arrStrDBVar[k]);
									vFinVar.add(arrStrDBVar[k]);
									if(k < arrStrDBVar.length) {
										strbNewTarget.append("/");
									}									
								}

							}

						}
						
						for(int k = 0; k < arrStrRef.length ; k++) {
							bRefIsInDB = false;
							for(int l = 0; l < arrStrDBVar.length && !bRefIsInDB; l++) {
								if(!vFinVar.contains(arrStrRef[k]) &&
									!isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef) && 
									(arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l])
									|| (arrStrDBVar[l].contains(".") && 
									arrStrRef[k].equalsIgnoreCase(arrStrDBVar[l].substring(0,
									arrStrDBVar[l].indexOf(".")))))) {
									strbNewTarget.append(arrStrRef[k]);
									bRefIsInDB = true;
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewTarget.append("/");
									}
								}else if(!vFinVar.contains(arrStrDBVar[l]) && 
									!isWholePresent(arrStrDBVar[l], arrStrDBVar, arrStrRef) && 
									arrStrRef[k].contains(".") && 
									arrStrDBVar[l].equalsIgnoreCase(arrStrRef[k].substring(0,
									arrStrRef[k].indexOf(".")))) {
									strbNewTarget.append(arrStrDBVar[l]);
									bRefIsInDB = true;
									vFinVar.add(arrStrDBVar[l]);
									if(k < arrStrRef.length) {
										strbNewTarget.append("/");
									}
								}
							}

							if((!vFinVar.contains(arrStrRef[k])
								|| (arrStrRef[k].contains(".")
								&& !vFinVar.contains(arrStrRef[k].substring(0,
								arrStrRef[k].indexOf(".")))))
								&& !isWholePresent(arrStrRef[k], arrStrDBVar, arrStrRef)
								&& !bRefIsInDB) {
								
								if(!vFinVar.contains(arrStrRef[k])){
									strbNewTarget.append(arrStrRef[k]);
									vFinVar.add(arrStrRef[k]);
									if(k < arrStrRef.length) {
										strbNewTarget.append("/");
									}									
								}

							}
						}
						
						while(strbNewTarget.toString().endsWith("/")) {
							strbNewTarget.deleteCharAt(strbNewTarget.length()-1);
						}
						
						strbNewTarget.append("]");
					}else {
						strbNewTarget.append(oTmp.toString());
					}
					strbDBTarget.append(oTmp.toString());
					strbDBTarget.append(" ");
					strbNewTarget.append(" ");
				}
			}

//		update the db with the refinement vars in the vars of tDB
		if(vSTemplatesFrDB.size() > 0 && vTTemplatesFrDB.size() > 0) {

			String sUpdate = "UPDATE chunks SET "
				+ "chunk" + strSource + " = '" 
				+ strbNewSource.toString().trim().replace("'","''") + "', "
				+ "chunk" + strTarget + " = '" 
				+ strbNewTarget.toString().trim().replace("'","''") + "' "
				+ "WHERE "+ "chunk" + strSource + " = '" 
				+ strbDBSource.toString().trim().replace("'","''") + "' AND "
				+ "chunk" + strTarget + " = '" 
				+ strbDBTarget.toString().trim().replace("'","''") + "' AND "
				+ "compID" + strSource + " = '" 
				+ tSourceRefinement.getCompoundIdentifier() 
				+ "' AND compID" + strTarget + " = '" 
				+ tTargetRefinement.getCompoundIdentifier() 
				+ "'";
			println(sUpdate);
			DBConnector.update(sUpdate);

			tNewSource = new TExtChunk(strbNewSource.toString().trim(),
						tSourceRefinement.getCompoundIdentifier());
			tNewTarget = new TExtChunk(strbNewTarget.toString().trim(),
						tTargetRefinement.getCompoundIdentifier());
			

			atNewRefinement = new AlignedChunk(tNewSource, tNewTarget);
		}

		oTmp = null;
		oTmpRef = null;
		tTmp = null;
		vSTemplatesFrDB = null;
		vTTemplatesFrDB = null;
		strbSRegexp = null;
		strbTRegexp = null;
		strbNewSource = null;
		strbNewTarget = null;
		rsDomain = null;

		return atNewRefinement;
	}

	protected boolean isWholePresent(String strToBeChecked, String[] arrStrDBVar,
		String[] arrStrRef) {
		boolean bFound = false;
		String strTmp = null;
		
		if(strToBeChecked.contains(".")) {
			strTmp = strToBeChecked.substring(0, strToBeChecked.indexOf("."));
			
			for(int i = 0; i < arrStrDBVar.length && !bFound; i++) {
				if(strTmp.equals(arrStrDBVar[i])) {
					bFound = true;
				}
			}
	
			for(int i = 0; i < arrStrRef.length && !bFound; i++) {
				if(strTmp.equals(arrStrRef[i])) {
					bFound = true;
				}
			}
			
		}
		
		return bFound;	
			
	}
										
	/**
	 *	This function strips the square brackets 
	 *	if the given domain still have them.
	 *
	 *	@param String strDomain (ex. [1], [1.1], [1/2.1])
	 *	@return String - with no square brackets at the start and at the end
	 *	(ex. 1, 1.1, 1/2.1)
	 */
	protected String stripSquareBrackets(String strDomain) {
		
		if(strDomain != null && strDomain.startsWith("[") && strDomain.endsWith("]")) {
			//strip the [] off
			strDomain = strDomain.substring(1, strDomain.length() - 1);
		}
		
		return strDomain;
	}

	
	
	//is the chunk a domain and is it part of the given domain?
	protected boolean isChunkADomAndInDomain(String strChunk, String strDom) {
		boolean bChunkIsDomAndInDomain = false;
		
		String[] arrStrDomains = stripSquareBrackets(strDom).split("/");
		String[] arrStrChunks = stripSquareBrackets(strChunk).split("/");
		
		for(int i = 0; i < arrStrDomains.length && !bChunkIsDomAndInDomain; i++) {
			for(int j = 0; j < arrStrChunks.length && !bChunkIsDomAndInDomain; j++) {
				if(arrStrDomains[i].equalsIgnoreCase(arrStrChunks[j])
					|| (arrStrChunks[j].contains(".") && 
					arrStrDomains[i].equalsIgnoreCase(arrStrChunks[j].substring(0,
					arrStrChunks[j].indexOf("."))))){
					bChunkIsDomAndInDomain = true;
				}
			}
		}
		
		arrStrDomains = null;
		arrStrChunks = null;
		
		return bChunkIsDomAndInDomain;
	}

	/**
	 *	This function retrieves the first whole number seen in the given domain.
	 *	For domains which does not contain a whole number, null would be returned.
	 *	
	 *	@param String strDomain (ex. [1], [1.1], [1/2.1], [1/1.1/2])
	 *	@return String - the first whole number seen in the given domain
	 *	(ex. 1, null, 1, 1)
	 */
	protected String getWholeNoDomain(String strDomain) {

		String[] arrStrDomains = stripSquareBrackets(strDomain).split("/");
		String strWholeNoDom = null;
		boolean bFound = false;
		
		for(int i = 0; i < arrStrDomains.length && !bFound; i++) {
			if(!arrStrDomains[i].contains(".")) {
				strWholeNoDom = arrStrDomains[i];
				bFound = true;
			}
		}
		
		arrStrDomains = null;
		
		return strWholeNoDom;
	}

	protected TExtChunk modOrigin(TExtChunk tSChunk, TExtChunk tTChunk, 
		String strOrigDom, String strNewDom, String strSource, String strTarget,
		boolean bModSource) {
		//find the strOrigDom and change it

		TExtChunk tChunk = null;
		String strLang = null;
		String strID = null;	
		Object oTmp = null;
		StringBuffer strbChunk = new StringBuffer();
		ResultSet rsID = null;
	
		if(bModSource) {
			tChunk = tSChunk;
			strLang = strSource;
		}else {
			tChunk = tTChunk;
			strLang = strTarget;
		}

//		System.out.println("tChunk: "+tChunk);
//		System.out.println("strOrigDom: "+strOrigDom);
//		System.out.println("strNewDom: "+strNewDom);

		strOrigDom = stripSquareBrackets(strOrigDom);
		strNewDom = stripSquareBrackets(strNewDom);

		for(int i = 0; i < tChunk.size(); i++) {
			oTmp = tChunk.get(i);
			if(oTmp instanceof TExtChunk) {
//				System.out.println("oTmp: "+oTmp);
				if(((TExtChunk)oTmp).toString().equalsIgnoreCase("["+strOrigDom+"]")) {
					strbChunk.append(((TExtChunk)oTmp).toString().replace("["+strOrigDom+"]",
						"["+strNewDom.substring(0,strNewDom.indexOf("."))+"/"+strOrigDom+"]"));
				}else {
					strbChunk.append(oTmp);
				}
			}else {
				strbChunk.append(oTmp);
			}
			strbChunk.append(" ");
		}
		
		rsID = DBConnector.select("SELECT  chunkID"
			+ " FROM Chunks WHERE "
			+ "chunk" + strSource + " = '" 
			+ tSChunk.toString().replace("'","''") + "'"
			+ " AND chunk" + strTarget + " = '" 
			+ tTChunk.toString().replace("'","''") + "'"
			+ " AND compID" + strSource + " = '" 
			+ tSChunk.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '" 
			+ tTChunk.getCompoundIdentifier() + "'");

		try {
			if(rsID.next()) {
				strID = rsID.getString("chunkID");
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();
		}

		//update the template of the origin
		DBConnector.update("UPDATE Chunks SET "
			+ "chunk" + strLang + " = '" + strbChunk.toString().trim().replace("'","''") + "'"
			+ " WHERE chunkID = '" 
			+ strID + "'");
		
		tChunk = null;
		strLang = null;
		strID = null;	
		oTmp = null;
		rsID = null;
		
		return new TExtChunk(strbChunk.toString().trim(), tSChunk.domain());
	}

	/**
	 *	This function processes the slashes in the given domain and retrieves 
	 *	all the chunks in the DB that have the same domain as the given domain.
	 *	
	 *	The given domain is separated by slashes.
	 *
	 *	@param String strDomain (ex. [1], [1.1], [1/2.1], [1/1.1/2])
	 *	@param String strSource - the source language (ex. English, Filipino)
	 *	@param String strTarget - the target language (ex. Filipino, English)
	 *	@return Vector - containing the Vector of source and Vector of target TExtChunk(s)
	 */
	protected Vector getChunksFrDB(String strDomain, 
		String strSource, String strTarget) {
		
		String[] arrStrDomains = stripSquareBrackets(strDomain).split("/");
		Vector vSrcAndTarChunksDB = null;
		Vector vSChunksFrDB = new Vector();
		Vector vTChunksFrDB = new Vector();
		Vector vTmpDB, vTmpS, vTmpT;
		
		for(int i = 0; i < arrStrDomains.length; i++) {
			vTmpDB = getChunksFrDBGivenADom(arrStrDomains[i], strSource, strTarget);
			
			if(vTmpDB != null) {
				vTmpS = (Vector)vTmpDB.get(0);
				vTmpT = (Vector)vTmpDB.get(1);
				
				for(int j = 0; j < vTmpS.size() && j < vTmpT.size(); j++) {
					vSChunksFrDB.add(vTmpS.get(j));
					vTChunksFrDB.add(vTmpT.get(j));
				}
			}
		}

		if(vSChunksFrDB.size() > 0 && vTChunksFrDB.size() > 0) {
			vSrcAndTarChunksDB = new Vector(2);
			vSrcAndTarChunksDB.add(vSChunksFrDB);
			vSrcAndTarChunksDB.add(vTChunksFrDB);
		}

		arrStrDomains = null;
		vSChunksFrDB = null;
		vTChunksFrDB = null;
		vTmpDB = null;
		vTmpS = null;
		vTmpT = null;

		return vSrcAndTarChunksDB;
	}

	/**
	 *	This function retrieves all the chunks in the DB that have the same 
	 *	domain as the given domain.
	 *	
	 *	@param String strDomain (ex. 1, 1.1)
	 *	@param String strSource - the source language (ex. English, Filipino)
	 *	@param String strTarget - the target language (ex. Filipino, English)
	 *	@return Vector - containing the Vector of source and Vector of target TExtChunk(s)
	 */
	protected Vector getChunksFrDBGivenADom(String strDomain,
		String strSource, String strTarget) {
		
		Vector vSrcAndTarChunksDB = null;
		Vector vSChunksFrDB = new Vector();
		Vector vTChunksFrDB = new Vector();
		ResultSet rsDomain = null;
		String strSrc = null;
		String strTar = null;
		float fDom;
		String strDom = null;
		String strCompIDS = null;
		String strCompIDT = null;
		
		if(strDomain.contains(".")) {
			rsDomain = DBConnector.select("SELECT chunk"+
				strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
				" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '"+strDomain+"%'");
		}else{
			rsDomain = DBConnector.select("SELECT chunk"+
				strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
				" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '" + strDomain + ".%'");
		}

		try {
			while(rsDomain.next()){
				strSrc = rsDomain.getString("chunk" + strSource);
				strTar = rsDomain.getString("chunk" + strTarget);
				fDom = rsDomain.getFloat("domain");
				strDom = fDom + "";
				strCompIDS = rsDomain.getString("compID" + strSource);
				strCompIDT = rsDomain.getString("compID" + strTarget);

				if(strSrc.trim().length() > 0 || strTar.trim().length() > 0) {
					vSChunksFrDB.add(tlu.adjustChunkCompound(new TExtChunk(strSrc, strDom, strCompIDS)));
					vTChunksFrDB.add(tlu.adjustChunkCompound(new TExtChunk(strTar, strDom, strCompIDT)));
				}
			}
		} catch(SQLException sqle){
			System.out.println("There is an SQL Error!");
			sqle.printStackTrace();
		}
		
		if(vSChunksFrDB.size() > 0 && vTChunksFrDB.size() > 0) {
			vSrcAndTarChunksDB = new Vector(2);
			vSrcAndTarChunksDB.add(vSChunksFrDB);
			vSrcAndTarChunksDB.add(vTChunksFrDB);
		}

		vSChunksFrDB = null;
		vTChunksFrDB = null;
		rsDomain = null;
		strSrc = null;
		strTar = null;
		strDom = null;
		strCompIDS = null;
		strCompIDT = null;

		return vSrcAndTarChunksDB;
	}

	//checks if the given TExtChunk is not of the form XY
	protected boolean chunkFrDBisNotXY(TExtChunk cChunksFrDB) {
		boolean bPresentXY = false;
		int nX = -1;

		for(int i = 0; i < cChunksFrDB.size() && !bPresentXY; i++) {
			if(cChunksFrDB.get(i) instanceof TExtChunk) {
				if(nX < 0) {
					nX = i;
				}else {
					if(nX == i - 1) {
						bPresentXY = true;
					}else {
						nX = i;
					}
				}
			}
		}
		
		return !bPresentXY;
	}
	
	/**
	 *	This function determines if the chunk in the database and the input 
	 *	chunk match each other.
	 *
	 *	The criteria of matching is that all constants in the database 
	 *	chunk corresponds to those constants in the input chunk.
	 *	Also at least one constant in the input chunk corresponds to
	 *	a variable in the database chunk.
	 *
	 *	For example:
	 *	[1]	Input Chunk: I love you
	 *		Database Chunk: [1] love [2]
	 *
	 *	[2] Input Chunk: They all love you very much.
	 *		Database Chunk: [1] love [2]
	 *
	 *	- ensures that every variable should have a constant
	 */
	protected boolean isMatch(TExtChunk cDB, TExtChunk cGiven){
		int nTrack = 0;
		Vector vPattern = new Vector();
		boolean bPrevIsChunk = false;
		boolean bHasMatch;
		boolean bContinue = true;
		for(int i = 0; i < cDB.size() && bContinue; i++){
			Object oCDBToken = cDB.get(i);
			if(oCDBToken instanceof String){
				bHasMatch = false;
				for(int j = nTrack; j < cGiven.size() && !bHasMatch; j++){
					Object oCGToken = cGiven.get(j);
					if(oCGToken.toString().compareTo(oCDBToken.toString()) == 0){
						nTrack = j;
						bHasMatch = true;
						bPrevIsChunk = false;
						do{
							vPattern.add("String");
							i++;
							nTrack++;
							if(i < cDB.size() && nTrack < cGiven.size()){
								oCDBToken = cDB.get(i);
								oCGToken = cGiven.get(nTrack);
								if(oCDBToken instanceof TExtChunk){
									i--;
									nTrack--;
									break;
								}
								else if(oCGToken.toString().compareTo(oCDBToken.toString()) != 0){
									bContinue = false;
									break;
								}
							}
							else{
								if(nTrack < cGiven.size())
									return false;
								break;
							}
						}while(i < cDB.size() && nTrack < cGiven.size());
						nTrack++;
					}
					else if(!bPrevIsChunk){
						bContinue = false;
						break;
					}
					else if(bPrevIsChunk)
						nTrack = j+1;
						
					oCGToken = null;
				}
			}
			else if(nTrack < cGiven.size()){
				vPattern.add("TExtChunk");
				nTrack++;
				bPrevIsChunk = true;
			}
			oCDBToken = null;
		}
		if(!bContinue)
			return false;
		if(nTrack < cGiven.size() && !bPrevIsChunk)
			return false;
		if(vPattern.size() != cDB.size()) {
			vPattern = null;
			return false;
		}
		return true;
	}
	
	/**
	 *	Look for the appropriate domains of those in the chunk 
	 *	given the chunk DB as its basis
	 *
	 *	@return Vector<AlignedChunk> contains the assigned Chunks of 
	 *	Source and Target with the appropriate Domain
	 *
	 *	if magmatch sa chunk -> pass the domain : 
	 *		means a there's a chunk in the DB similar to it
	 *	else -> null : means there is no chunk like that in the DB
	 *
	 *	Note:
	 *	possible optimization: storing all the values of the 
	 *	variable in the target and source
	 *
	 *	what if [1] and [1]
	 *	what if puro constants ung cSDB at cTDB?
	 *	what if mix ung cSDB at cTDB?
	 *	similarity for target
	 *	XY
	 *	1.1 -> 2
	 *
	 *	- what if variable lang ang magkaiba? sa isang chunk
	 *
	 *	- assumes: every var should have a value which is ensured in isMatch fcn
	 *
	 *	not tested
	 */
	protected Vector findChunkDomains(TExtChunk cSDB, TExtChunk cTDB,
		TExtChunk cSource, TExtChunk cTarget, String strSource, 
		String strTarget, Hashtable hAlignedWords, Vector vSourceChunks,
		Vector vTargetChunks) {

//		System.out.println("***********************************************");
//		System.out.println("FIND CHUNK DOM START");

		int nCount = 0;
		Vector vAlignedOriginAndChunksResult = new Vector(2),
			vAlignedChunksResult = null,
			vAlignedDBResult = null,
			vSPrelim = null,
			vTPrelim = null,
			vMatches = null;
		
		/**
		 *	know: cSDB matches cSource and cTDB matches cTarget
		 *	find: domain of cSource and cTarget
		 *	because there can be a variable in cSDB or cTDB
		 *
		 *	check if the cSDB == cSource && cTDB == cTarget
		 *	YES: exit... no need to look for chunk domains
		 *		 it's already in the DB
		 *	NO: continue
		 */
		if(cSDB.toString().equalsIgnoreCase(cSource.toString()) && 
			cTDB.toString().equalsIgnoreCase(cTarget.toString())) {
//			System.out.println("No need to add the chunk in the DB."
//				+ "It's already there.");
		}else {
			//count the number of variables
			//assumes: no. of var in cSDB == no. of var in cTDB
			nCount = countVar(cSDB);
			
			//get preliminary value of the variables in the source
			//consecutive vars are grouped as one here
			vSPrelim = getPrelimVarAndVal(cSDB, cSource);

			//get preliminary value of the variables in the target
			//consecutive vars are grouped as one here
			vTPrelim = getPrelimVarAndVal(cTDB, cTarget);
			
			//match those preliminary values of the variables of the 
			//source and target to each other through hAlignedWords
//			System.out.println("vSPrelim: "+vSPrelim);
//			System.out.println("vTPrelim: "+vTPrelim);

			vMatches = matchSourceAndTargetVars(cSDB, cSource, cTDB, cTarget,
				vSPrelim, vTPrelim, hAlignedWords);
//			System.out.println("vMatches: "+vMatches);

			//if vMatches does not equal the number of variables in the count
			//|| count is 0 then cDB can't be used as a basis for subchunks
			if(vMatches == null || vMatches.size() <= 0 ||
				nCount != vMatches.size()) {
				vAlignedOriginAndChunksResult = null;
			}else if(vAlignedOriginAndChunksResult != null){
				vAlignedOriginAndChunksResult.add(new Vector());
				vAlignedOriginAndChunksResult.add(new Vector());
				//check each source and target preliminary values for a
				//deeper subchunk (call this fcn again) if it does not 
				//have it then add it in the vector with the chunk origin
				//for-loop
				for(int nMatchIndex = 0; nMatchIndex < vMatches.size();
					nMatchIndex++) {
					AlignedChunk aMatch = (AlignedChunk)vMatches.get(nMatchIndex);
					TExtChunk cSrcSubChunk = (TExtChunk)aMatch.getSource();
					TExtChunk cTarSubChunk = (TExtChunk)aMatch.getTarget();
		 			String strDomain = cSrcSubChunk.domain();

	 				//get all chunks + domain in the DB with domain same as the domain of the Chunk
		 			Vector vChunksDB = getChunksFrDB(strDomain, strSource, strTarget);

		 			if(vChunksDB != null) {
						//storage of source TExtChunks in the DB
			 			Vector vSrcChunksFrDB = (Vector) vChunksDB.get(0);
						//storage of target TExtChunks in the DB
			 			Vector vTarChunksFrDB = (Vector) vChunksDB.get(1);
						boolean bMatch = false;

						TExtChunk cSrcChunksFrDB = null;
						TExtChunk cTarChunksFrDB = null;
						boolean bSChunkFrDBisNotXY;
						boolean bTChunkFrDBisNotXY;
						boolean bSIsMatch;
						boolean bTIsMatch;

						int nDBIndex = 0;
						//while all chunksDB && no match
						while(nDBIndex < vSrcChunksFrDB.size() && !bMatch) {
							//does the chunks in both source and target 
							//match to the chunks in the DB
							cSrcChunksFrDB = (TExtChunk)vSrcChunksFrDB.get(nDBIndex);
							cTarChunksFrDB = (TExtChunk)vTarChunksFrDB.get(nDBIndex);
							bSChunkFrDBisNotXY = chunkFrDBisNotXY(cSrcChunksFrDB);
							bTChunkFrDBisNotXY = chunkFrDBisNotXY(cTarChunksFrDB);
							bSIsMatch = isMatch(cSrcChunksFrDB, cSource);
							bTIsMatch = isMatch(cTarChunksFrDB, cTarget);

							if(bSChunkFrDBisNotXY &&
								bTChunkFrDBisNotXY &&
								bSIsMatch && 
								bTIsMatch) {
							
//					 			System.out.println("-->IS MATCH<--");
								//assign the domain of the chunks (S&T) based on the chunks in the DB
								Vector vAOriginAndChunksWithDomains = findChunkDomains((TExtChunk)vSrcChunksFrDB.get(nDBIndex),
											(TExtChunk)vTarChunksFrDB.get(nDBIndex), cSrcSubChunk, cTarSubChunk, strSource,
											strTarget, hAlignedWords, vSourceChunks, vTargetChunks);
								
								if(vAOriginAndChunksWithDomains != null &&
									vAOriginAndChunksWithDomains.size() > 0) {
									bMatch = true;
									//include those new in vAChunksWithDomains in the current vAlignedChunksResult
									vAlignedDBResult = (Vector)vAOriginAndChunksWithDomains.get(0);
									vAlignedChunksResult = (Vector)vAOriginAndChunksWithDomains.get(1);
									for(int i = 0; i < vAlignedChunksResult.size(); i++) {
										((Vector)vAlignedOriginAndChunksResult.get(0)).add(vAlignedDBResult.get(i));
										((Vector)vAlignedOriginAndChunksResult.get(1)).add(vAlignedChunksResult.get(i));
									}
								}//else: if vAChunksWithDomains or there are no subchunks to be added
								
								vAOriginAndChunksWithDomains = null;
							}
							nDBIndex++;
						}
						//no match means new domain
						if(!bMatch){
//			 				System.out.println("FCD: NEW DOMAIN! - no match");
			 				//strDomain: ex. if 1.1 
			 				//strSrcDomain: ex. 4 -> most previous so use this instead
			 				String strNewDom;

							if(getWholeNoDomain(strDomain) == null &&
								getWholeNoDomain(cSDB.domain()) != null) {
			 					strNewDom = checkDomain(cSDB.domain(),
									vSourceChunks, vTargetChunks, cSource, cTarget);

								//check if checkDomain changed in the whole number part
								//&& if X.Y is already in the DB: assume: X.Y already exists
				 				if(getWholeNoDomain(cSDB.domain()) == null &&
				 					!cSDB.domain().substring(0,
				 					cSDB.domain().indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
				 					strNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					cSDB = modOrigin(cSDB, cTDB, cSDB.domain(),
				 						strNewDom, strSource, strTarget, true);
				 					cTDB = modOrigin(cSDB, cTDB, cSDB.domain(),
				 						strNewDom, strSource, strTarget, false);
				 				}

			 				}else {
			 					strNewDom = checkDomain(strDomain,
									vSourceChunks, vTargetChunks, cSource, cTarget);

								//check if checkDomain changed in the whole number part
								//&& if X.Y is already in the DB: assume: X.Y already exists
				 				if(getWholeNoDomain(strDomain) == null &&
				 					!strDomain.substring(0,
				 					strDomain.indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
				 					strNewDom.indexOf(".")))) {
									//if it did then change the template or the chunk
				 					cSDB = modOrigin(cSDB, cTDB, strDomain,
				 						strNewDom, strSource, strTarget, true);
				 					cTDB = modOrigin(cSDB, cTDB, strDomain,
				 						strNewDom, strSource, strTarget, false);
				 				}

			 				}

							cSrcSubChunk.setDomain(strNewDom);
			 				cTarSubChunk.setDomain(strNewDom);

				 			((Vector)vAlignedOriginAndChunksResult.get(0)).add(new AlignedChunk(cSDB, cTDB));
							((Vector)vAlignedOriginAndChunksResult.get(1)).add(new AlignedChunk(cSrcSubChunk, cTarSubChunk));
							
			 				strNewDom = null;
						}

			 			vSrcChunksFrDB = null;
						vTarChunksFrDB = null;
						cSrcChunksFrDB = null;
						cTarChunksFrDB = null;
						
					}
					//else: chunksDB == null
					else {
		 				//NO. strDomain: ex. if 1.1 || 1 but no match inside those
		 				//NO. strSrcDomain: ex. 4 -> most previous so use this instead
//		 				System.out.println("FCD: NEW DOMAIN! - no match in db");
			 			String strNewDom;

						if(getWholeNoDomain(strDomain) == null &&
							getWholeNoDomain(cSDB.domain()) != null) {
		 					strNewDom = checkDomain(cSDB.domain(),
								vSourceChunks, vTargetChunks, cSource, cTarget);

							//check if checkDomain changed in the whole number part
							//&& if X.Y is already in the DB: assume: X.Y already exists
			 				if(getWholeNoDomain(cSDB.domain()) == null &&
			 					!cSDB.domain().substring(0,
			 					cSDB.domain().indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
			 					strNewDom.indexOf(".")))) {
								//if it did then change the template or the chunk
			 					cSDB = modOrigin(cSDB, cTDB, cSDB.domain(),
			 						strNewDom, strSource, strTarget, true);
			 					cTDB = modOrigin(cSDB, cTDB, cSDB.domain(),
			 						strNewDom, strSource, strTarget, false);
			 				}
		 				}else {
		 					strNewDom = checkDomain(strDomain,
								vSourceChunks, vTargetChunks, cSource, cTarget);

							//check if checkDomain changed in the whole number part
							//&& if X.Y is already in the DB: assume: X.Y already exists
			 				if(getWholeNoDomain(strDomain) == null &&
			 					!strDomain.substring(0,
			 					strDomain.indexOf(".")).equalsIgnoreCase(strNewDom.substring(0,
			 					strNewDom.indexOf(".")))) {
								//if it did then change the template or the chunk
			 					cSDB = modOrigin(cSDB, cTDB, strDomain,
			 						strNewDom, strSource, strTarget, true);
			 					cTDB = modOrigin(cSDB, cTDB, strDomain,
			 						strNewDom, strSource, strTarget, false);
			 				}
		 				}

						cSrcSubChunk.setDomain(strNewDom);
		 				cTarSubChunk.setDomain(strNewDom);

			 			((Vector)vAlignedOriginAndChunksResult.get(0)).add(new AlignedChunk(cSDB, cTDB));
						((Vector)vAlignedOriginAndChunksResult.get(1)).add(new AlignedChunk(cSrcSubChunk, cTarSubChunk));

						strNewDom = null;
					}
					
					aMatch = null;
					cSrcSubChunk = null;
					cTarSubChunk = null;
		 			strDomain = null;
					vChunksDB = null;

				}//for-loop for vMatch
			}//else if strChunk is empty: don't know yet -> error?: should be impossible
		}
		
		if(vAlignedOriginAndChunksResult == null ||
			vAlignedOriginAndChunksResult.size() <= 0 || 
			((Vector)vAlignedOriginAndChunksResult.get(1)).size() <= 0) {
			vAlignedOriginAndChunksResult = null;
		}
		
//		System.out.println("***********************************************");
//		System.out.println(vAlignedOriginAndChunksResult+"\nFIND CHUNK DOM END");

		vAlignedChunksResult = null;
		vAlignedDBResult = null;
		vSPrelim = null;
		vTPrelim = null;
		vMatches = null;
		
		return vAlignedOriginAndChunksResult;
	}
	
	//counts the variable
	protected int countVar(TExtChunk cDB) {
		int nCount = 0;
		
		for(int i = 0; i < cDB.size(); i++) {
			if(cDB.get(i) instanceof TExtChunk) {
				nCount++;
			}
		}
		
		return nCount;
	}

	//var is from DB, val is from chunk
	//gets the vars' possible val through the constants
	//if the var is of the form XY all vars are in one Vector
	protected Vector getPrelimVarAndVal(TExtChunk cDB, TExtChunk cChunk) {
		
		boolean bCantAddSubchunks = false,
			bFoundNextConstant;
		int nChunkIndex = 0;
		Vector vPrelim = new Vector(),
			vValAndVar = null,
			vVariable = null,
			vValue = null;
		String strCurrentCDB = null,
			strCurrentChunk = null,
			strNextCDB = null,
			strNextChunk = null;
		
		for(int nDBIndex = 0; nDBIndex < cDB.size() &&
			nChunkIndex < cChunk.size() && !bCantAddSubchunks; nDBIndex++) {
			
			strCurrentCDB = cDB.get(nDBIndex) + "";

			//if the String in the ChunkDB is constant
			if(!strCurrentCDB.startsWith("[") && !strCurrentCDB.endsWith("]")) {
				strCurrentChunk = cChunk.get(nChunkIndex) + "";
				//if the constant does not match each other
				if(!strCurrentCDB.equalsIgnoreCase(strCurrentChunk)) {
					bCantAddSubchunks = true;
					vPrelim = null;
				}
				nChunkIndex++;
			}
			//if the String in the ChunkDB is a variable
			else {
				//expand variable
				bFoundNextConstant = false;
				vValAndVar = new Vector(2);
				vVariable = new Vector();
				vValue = new Vector();
				vVariable.add(strCurrentCDB);

				while(nDBIndex < cDB.size() && !bFoundNextConstant) {
					//if there is a next in cDB
					if(nDBIndex < cDB.size()-1) {
						strNextCDB = cDB.get(nDBIndex + 1) + "";
						//check if the next String in the ChunkDB is a constant
						if(!strNextCDB.startsWith("[") && 
							!strNextCDB.endsWith("]")) {
							//get those in between in the cChunk from current
							//index to the mark of cDB
							//not yet the last String && not equal to the next
							//String in the cDB which is a constant
						
							while(nChunkIndex < cChunk.size() && 
								!strNextCDB.equalsIgnoreCase(strCurrentChunk = new String(cChunk.get(nChunkIndex)
								+""))) {

								vValue.add(strCurrentChunk);
								nChunkIndex++;
							}
							
							vValAndVar.add(vVariable);
							vValAndVar.add(vValue);
							bFoundNextConstant = true;
						}
						//[X][Y] collects the variables
						else {
							vVariable.add(strNextCDB);
						}
					//if last
					}else {
						//get the Strings in the chunk before the 
						//constant in the chunkDB or the subchunk
						//not yet the last String && not equal to
						//the next String in the chunkDB 
						//which is a constant
						while(nChunkIndex < cChunk.size()) {
							strCurrentChunk = cChunk.get(nChunkIndex)+"";
						
							vValue.add(strCurrentChunk);
							nChunkIndex++;
						}

						vValAndVar.add(vVariable);
						vValAndVar.add(vValue);
						bFoundNextConstant = true;
					}//else if strChunk is empty: don't know yet -> error?: 
					//should be impossible
					nDBIndex++;
				}//while: find the next constant or the end in the cDB
				nDBIndex--;
				vPrelim.add(vValAndVar);
			}//else EXPAND VAR
		}//for-loop for the cDB
		
		vValAndVar = null;
		vVariable = null;
		vValue = null;
		strCurrentCDB = null;
		strCurrentChunk = null;
		strNextCDB = null;
		strNextChunk = null;

		if(vPrelim.size() <= 0) {
			vPrelim = null;
		}
		
		return vPrelim;
	}

	//assumes: source and target may match each 
	//other from the division of preliminary values
	protected Vector matchSourceAndTargetVars(TExtChunk cDB1, TExtChunk cChunk1,
		TExtChunk cDB2, TExtChunk cChunk2, Vector vPrelim1, Vector vPrelim2,
		Hashtable hAlignedWords) {

//		System.out.println("************************************");
//		System.out.println("START - matchSourceAndTargetVars");
	
		boolean bCantAddSubChunk = false;
			//bNoPrevMatches = false;
		int n2PrelimIndex;
		Vector vMatch = new Vector(),	//AlignedChunk: value and domain
			vVariable1 = null,
			vValue1 = null,
			vVariable2 = null,
			vValue2 = null,
			vN2PrelimIndexMatched = new Vector();
		String strVar1 = null;
		Vector vSrcSubChunk = null,
			vTarSubChunk = null,
			vSubChunkN2PrelimIndexMatched = null;
		
		if(vPrelim1 != null && vPrelim2 != null) {
			//note: vPrelim1.size() != vPrelim2.size() if with XY
			//in this case since only separate vars then they should be equal
			//store the n2PrelimIndex which is already matched to avoid redundancy
			for(int n1PrelimIndex = 0; n1PrelimIndex < vPrelim1.size() &&
				!bCantAddSubChunk; n1PrelimIndex++) {
				vVariable1 = (Vector)((Vector)vPrelim1.get(n1PrelimIndex)).get(0);
				vValue1 = (Vector)((Vector)vPrelim1.get(n1PrelimIndex)).get(1);
				//assume: v1/2Variable always has at least one value
				//if there is only one var in 1
				if(vVariable1.size() == 1) {
					strVar1 = stripSquareBrackets((String)vVariable1.get(0));
//					System.out.println("strVar1 : "+strVar1);
					//find the corresponding var in 2 (first instance)
					//checks for the existence of a particular variable
					n2PrelimIndex = getCorrespVarIndex(strVar1, vPrelim2);

					if(n2PrelimIndex >= 0) {
						vVariable2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(0);
						vValue2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(1);

						//if only one var
						if(vVariable2.size() == 1) {
							//check for alignment and find the suitable var
							vSrcSubChunk = (Vector)((Vector)vPrelim1.get(n1PrelimIndex)).get(1);
							vSubChunkN2PrelimIndexMatched = getAlignedValue(vVariable1,
										vValue1, vPrelim2, vN2PrelimIndexMatched,
										hAlignedWords, true);
							if(vSrcSubChunk != null && vSubChunkN2PrelimIndexMatched != null &&
								(vTarSubChunk = (Vector)vSubChunkN2PrelimIndexMatched.get(1)) != null) {
								TExtChunk cSrcSubChunk = new TExtChunk(tlu.toStringChunk(vSrcSubChunk), strVar1, tlu.makeCompoundIdentifier(vSrcSubChunk));
								TExtChunk cTarSubChunk = new TExtChunk(tlu.toStringChunk(vTarSubChunk), strVar1, tlu.makeCompoundIdentifier(vTarSubChunk));

								vMatch.add(new AlignedChunk(cSrcSubChunk, cTarSubChunk));
								vN2PrelimIndexMatched.add(((Integer)vSubChunkN2PrelimIndexMatched.get(0)));
							}else {
								//no XY
								//maybe the 2nd var is part of an XY or maybe
								//ambigiuous this will be addressed here or in
								//matchXY
								bCantAddSubChunk = true;
								vMatch = null;
							}
						}else {
							bCantAddSubChunk = true;
							vMatch = null;
						}
					}else {
						//the var1 is not in vPrelim2
						bCantAddSubChunk = true;
						vMatch = null;
					}
				}else {
					bCantAddSubChunk = true;
					vMatch = null;
				}
			}
		}
		
		if(vMatch != null && (vMatch.size() <= 0 || bCantAddSubChunk)) {
			vMatch = null;
		}
		
//		System.out.println("END - matchSourceAndTargetVars");
//		System.out.println("************************************");

		vVariable1 = null;
		vValue1 = null;
		vVariable2 = null;
		vValue2 = null;
		vN2PrelimIndexMatched = null;
		strVar1 = null;
		vSrcSubChunk = null;
		vTarSubChunk = null;
		vSubChunkN2PrelimIndexMatched = null;

		return vMatch;
	}
	
	//find the corresponding var in 2 (first instance)
	//assumes: strVar = [2] or strVar = [2.1] or strVar [3/4.1]
	protected int getCorrespVarIndex(String strVar, Vector vPrelim) {
		int nIndex = -1;
		boolean bFound = false;
		String strVariable = null;
		Vector vVariable = null;
		Vector vValue = null;
		
		for(int nPrelimIndex = 0; nPrelimIndex < vPrelim.size();
			nPrelimIndex++) {

			vVariable = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(0);
			vValue = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(1);
			
			for(int nVarIndex = 0; nVarIndex < vVariable.size() && !bFound;
				nVarIndex++) {
					
				strVariable = vVariable.get(nVarIndex) + "";
			
				if(strVariable.contains(strVar)) {
					nIndex = nVarIndex;
					bFound = true;
				}
			}
		}
		
		strVariable = null;
		vVariable = null;
		vValue = null;
		
		return nIndex;
	}

	//returns the value of a particular index
	protected String getValOfVar(int nPrelimIndex, Vector vPrelim) {
		Vector vVariable = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(0),
			vValue = (Vector)((Vector)vPrelim.get(nPrelimIndex)).get(1);
		StringBuffer strbValue = new StringBuffer();
		
		for(int i = 0; i < vValue.size(); i++) {
			strbValue.append(vValue.get(i));
			strbValue.append(" ");
		}
		
		
		String strValue = null;
		if(strbValue != null && strbValue.length() > 0) {
			strValue = strbValue.toString().trim();
		}
		
		vVariable = null;
		vValue = null;
		strbValue = null;
		
		return strValue;
	}
	
	//returns the aligned value of a corresponding var
	//compare vMatch
	//assumes: vVariable1 is only one var but vVariable2 may have n vars
	//XY alignment
	//previous match XY
	protected Vector getAlignedValue(Vector vVariable1, Vector vValue1,
		Vector vPrelim2, Vector vPrevPrelim2Matches, Hashtable hAlignedWords,
		boolean bIsSource) {
		
		boolean bCantAddSubChunk = false,
			bAligned = false;
		int nChosenIndex = -1;
		
		String strValue = null;
		Vector vAlignedIndex = new Vector(),	//AlignedTExtChunk: value and domain
			vAlignedValue = new Vector(),
			vVariable2 = null,
			vValue2 = null;					//possible XY
		
		if(vPrelim2 != null) {
			//note: vPrelim1.size() != vPrelim2.size()
			for(int n2PrelimIndex = 0; n2PrelimIndex < vPrelim2.size() &&
				!bCantAddSubChunk && !bAligned; n2PrelimIndex++) {
				vVariable2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(0);
				vValue2 = (Vector)((Vector)vPrelim2.get(n2PrelimIndex)).get(1);
				
				//what if XY? dpt meron lng na in common; ok lng sa ngayon kc wla munang XY
				if(!isIndexInVector(vPrevPrelim2Matches, n2PrelimIndex)
					&& isEquivVar(vVariable1, vVariable2)) {
					if(vVariable2.size() == 1) {
						//checkIfAligned
						//maybe all aligned
						//maybe not all r aligned bcoz of it being null
						if(bIsSource) {
							int nIndex = -1;
							boolean bFound = false;
							int nAligned = 0;
							StringBuffer strbAlWTarget = null;
							Vector vAlWTarget = null;
							for(int nVal1 = 0; nVal1 < vValue1.size(); nVal1++) {
								vAlWTarget = (Vector)hAlignedWords.get((String)vValue1.get(nVal1));
								if(vAlWTarget != null) {
									//search in value2
									//check if same order...
									for(int nVal2 = 0; nVal2 < vValue2.size() && !bFound; nVal2++) {
										//checkForDuplicateIndices
										if(!isIndexInVector(vAlignedIndex, nVal2)) {
											strbAlWTarget = new StringBuffer();
											for(int nAlWTarget = 0; nAlWTarget < vAlWTarget.size(); nAlWTarget++) {
												strbAlWTarget.append((String)vAlWTarget.get(nAlWTarget)+" ");
											}
											
											if(strbAlWTarget.toString().trim().equalsIgnoreCase((String)vValue2.get(nVal2))) {
												bFound = true;
												nIndex = nVal2;
											}
											if(nAligned == vAlWTarget.size()){
												bFound = true;
												nIndex = nVal2;
											}
										}//find another
									}
									//check if 
									if(bFound) {
										vAlignedIndex.add(new Integer(nIndex+""));
										bFound = false;
									}
								}else {
									vAlignedIndex.add(new Integer(nIndex+""));
								}
							}
							
							//???problem: what if source ung null at target meron - di align
							//ask kat: what if null sa target, ano ang sa source ng hash?
							//check for vals in vValue2 that has no alignment yet
							boolean bNotAligned = false;
							if(vAlignedIndex.size() < vValue2.size()) {
								for(int nVal2 = 0; nVal2 < vValue2.size() 
									&& !bNotAligned; nVal2++) {
									if(!isIndexInVector(vAlignedIndex, nVal2)) {
										//check: if may ganung word sa hash, kpg meron, then wrong 
										//so hindi aligned
										if(hAlignedWords.containsValue(((String) vValue2.get(nVal2)))) {
											bNotAligned = true;
										}else {
											vAlignedIndex.add(new Integer(nVal2));
										}
									}
								}
							}
							
							//if all in value2 is aligned then add its value 2 the buffer
							if(vAlignedIndex != null && vAlignedIndex.size() > 0
								&& vAlignedIndex.size() == vValue2.size()) {
								vAlignedValue = vValue2;
								bAligned = true;
								nChosenIndex = n2PrelimIndex;
							}

							strbAlWTarget = null;
							vAlWTarget = null;
						}
					}else {
						bCantAddSubChunk = true;
						vAlignedIndex = null;
					}
				}//else: it can't be aligned yet, maybe further on
			}
		}

		Vector vIndexNAlignedVal = new Vector();

		if(nChosenIndex >= 0 && vAlignedValue != null) {
			vIndexNAlignedVal.add(new Integer(nChosenIndex+""));
			vIndexNAlignedVal.add(vAlignedValue);
			vIndexNAlignedVal.trimToSize();
		}else {
			vIndexNAlignedVal = null;
		}

		strValue = null;
		vAlignedIndex = null;
		vAlignedValue = null;
		vVariable2 = null;
		vValue2 = null;

		return vIndexNAlignedVal;
	}
	
	protected boolean isIndexInVector(Vector vVector, int nIndex) {
		boolean bIsIn = false;
		
		if(vVector != null) {
			for(int i = 0; i < vVector.size() && !bIsIn; i++) {
				if(((Integer)vVector.get(i)).intValue() == nIndex) {
					bIsIn = true;
				}
			}
		}
		
		return bIsIn;
	}
	
	protected boolean isEquivVar(Vector vVariable1, Vector vVariable2) {
		boolean bEquiv = true;
		
		if(vVariable1 != null && vVariable2 != null && vVariable1.size() > 0
		   && vVariable2.size() > 0) {
			for(int i = 0; i < vVariable1.size() && bEquiv; i++) {
				if(!((String)vVariable1.get(i)).equalsIgnoreCase((String)vVariable2.get(i))) {
					bEquiv = false;
				}
			}
		}else {
			bEquiv = false;
		}
		
		return bEquiv;
	}

	/**
	 *	Assumption:
	 *		- the size of vSrcChunks at vTarChunks are equal
	 *
	 *	This function retrieves the highest domain in the domain of the given 
	 *	chunks.
	 *
	 *	@param Vector vSrcChunks
	 *	@param Vector vTarChunks
	 *	@return String containing the highest domain in the given chunks
	 *	(ex. 8, 8.5)
	 *
	 */
	protected String getMaxDomInChunks(Vector vSrcChunks, Vector vTarChunks, 
		TExtChunk cSource, TExtChunk cTarget) {

		float fMaxDom = -1;
		float fSDomain;
		float fTDomain;
		int nWholeDom;
		String strMaxDom = null,
			strSrcDom = null,
			strTarDom = null;
		TExtChunk cVSrc = null,
			cVTar = null;
		
		if(vSrcChunks != null && vTarChunks != null) {
			for(int i = 0; i < vSrcChunks.size(); i++) {
				cVSrc = (TExtChunk) vSrcChunks.get(i);
				cVTar = (TExtChunk) vTarChunks.get(i);
				
				if(cSource == null || cTarget == null ||
				!cVSrc.toString().equalsIgnoreCase(cSource.toString()) || 
				!cVTar.toString().equalsIgnoreCase(cTarget.toString())) {
					strSrcDom = stripSquareBrackets(cVSrc.domain());
					strTarDom = stripSquareBrackets(cVTar.domain());
					
					String[] arrStrSDom = strSrcDom.split("/");
					String[] arrStrTDom = strTarDom.split("/");
					
					for(int j = 0; j < arrStrSDom.length; j++) {
						if(arrStrSDom[j].contains(".")){
							nWholeDom = (new Integer(arrStrSDom[j].substring(0,arrStrSDom[j].indexOf(".")))).intValue();
							String strTmpMax = String.valueOf(fMaxDom);
							int nTmpMaxDec = (new Integer(strTmpMax.substring(arrStrSDom[j].indexOf(".") + 1, strTmpMax.length()))).intValue();
							if(nWholeDom > nTmpMaxDec){
								fMaxDom = (new Float(arrStrSDom[j])).floatValue();
							}
							//check .X
							else if(nWholeDom == nTmpMaxDec){
								strTmpMax = new String(fMaxDom+"");
								nTmpMaxDec = (new Integer(strTmpMax.substring(arrStrSDom[j].indexOf(".") + 1, strTmpMax.length()))).intValue();
								int nDecDom = (new Integer(arrStrSDom[j].substring(arrStrSDom[j].indexOf(".") + 1, arrStrSDom[j].length()))).intValue();
								
								if(nDecDom > nTmpMaxDec) {
									fMaxDom = (new Float(arrStrSDom[j])).floatValue();
								}
							}
							strTmpMax = null;
						}
						//whole no
						else{
							nWholeDom = (new Integer(arrStrSDom[j])).intValue();
							if(nWholeDom > fMaxDom){
								fMaxDom = (new Float(arrStrSDom[j])).floatValue();
							}
						}
					}
					
					for(int j = 0; j < arrStrTDom.length; j++) {
						if(arrStrTDom[j].contains(".")){
							nWholeDom = (new Integer(arrStrTDom[j].substring(0,arrStrTDom[j].indexOf(".")))).intValue();
							if(nWholeDom > fMaxDom){
								fMaxDom = (new Float(arrStrTDom[j])).floatValue();
							}
							//check .X
							else if(nWholeDom == fMaxDom){
								String strTmpMax = String.valueOf(fMaxDom);
								int nTmpMaxDec = (new Integer(strTmpMax.substring(arrStrTDom[j].indexOf(".") + 1, strTmpMax.length()))).intValue();
								int nDecDom = (new Integer(arrStrTDom[j].substring(arrStrTDom[j].indexOf(".") + 1, arrStrTDom[j].length()))).intValue();
								
								if(nDecDom > nTmpMaxDec) {
									fMaxDom = (new Float(arrStrTDom[j])).floatValue();
								}
								strTmpMax = null;
							}
						}
						//whole no
						else{
							nWholeDom = (new Integer(arrStrTDom[j])).intValue();
							if(nWholeDom > fMaxDom){
								fMaxDom = (new Float(arrStrTDom[j])).floatValue();
							}
						}
					}

					arrStrSDom = null;
					arrStrTDom = null;
					
				}
			}
		}
		
		if(fMaxDom > 0) {
			strMaxDom = fMaxDom + "";
			
			if(strMaxDom.endsWith(".0")) {
				strMaxDom = strMaxDom.substring(0,
					strMaxDom.indexOf("."));
			}
		}

		strSrcDom = null;
		strTarDom = null;
		cVSrc = null;
		cVTar = null;

		return strMaxDom;
	}

	/**
	 *	This function retrieves the highest domain in the database.
	 *
	 *	@return String containing the highest domain in the DB
	 *	(ex. 8.5)
	 */
	protected String getMaxDomInDB() {
		int nMax = Integer.parseInt(tlu.returnNextChunk())-1;
		
//		System.out.println("nMax -->" + nMax);
		
		String strMaxDomain = null;

		if(nMax == 0)
			return strMaxDomain;
		
		String strQuery = "SELECT domain FROM Chunks WHERE domain LIKE '" + nMax + ".%'";

		int nMaxLength = 0;
		Vector vDomains = new Vector();
		try{
			ResultSet rs = DBConnector.select(strQuery);
			while(rs.next()){
				String strDomain = rs.getString("domain");
				
//				System.out.println("strDomain -->" + strDomain);
				
				if(strDomain == null)
					break;
				int nDomLength = strDomain.length();
				if(nDomLength > nMaxLength){
//					System.out.println(nDomLength + ">" + nMaxLength);
					nMaxLength = nDomLength;
					vDomains.clear();
					vDomains.add(strDomain);
//					System.out.println("vDomains-->" + vDomains);
				}
				else if(nDomLength == nMaxLength){
//					System.out.println(nDomLength + "==" + nMaxLength);
					vDomains.add(strDomain);
//					System.out.println("vDomains-->" + vDomains);
				}
//				else
//					System.out.println(nDomLength + "<" + nMaxLength);
			}
			
			rs = null;
		}catch(Exception sqle){
			System.out.println("There is an SQL Error!");
			sqle.printStackTrace();
		}
		
		int nMaxDomain = 0;
//		System.out.println("vDomains-->" + vDomains);
		for(Iterator it = vDomains.iterator(); it.hasNext(); ){
//			System.out.println("nMaxDomain -->" + nMaxDomain);
//			System.out.println("strMaxDomain -->" + strMaxDomain);
			String strTemp = (String)it.next();
//			System.out.println("strTemp -->" + strTemp);

			int nTemp = Integer.parseInt(strTemp.substring(strTemp.indexOf(".") + 1, strTemp.length()));
//			System.out.println("nTemp -->" + nTemp);
			if(nTemp > nMaxDomain){
//				System.out.println("nTemp > nMaxDomain");
				nMaxDomain = nTemp;
				strMaxDomain = strTemp;
			}
//			else{
//				System.out.println("nTemp <= nMaxDomain");
//			}
		}
		
		strQuery = null;
		vDomains = null;

		return strMaxDomain;
	}

	/**
	 *	? Will depend on addChunkToDB
	 *
	 *	Assumptions:
	 *		- strDomain contains only one domain, no slashes
	 *
	 *	This function provides the new domain.
	 *	
	 *	For a given domain with a whole number, the next to 
	 *	the highest domain under that domain would be returned.
	 *	(ex. in the DB: 1.1, 1.2, 1.3; param: 1; returned: 1.4)
	 *
	 *	For a given domain with a decimal point, the next whole number with
	 *	respect to the domains in the database and in all the given chunks
	 *	with .1 would be returned.
	 *	(ex. in the DB: 1.1, 1.2, 1.3, 2.1, 2.2; param: 2.2; returned: 3.1)
	 *
	 */
	protected String getNewDomain(String strDomain, Vector vSrcChunks,
		Vector vTarChunks, TExtChunk cSource, TExtChunk cTarget) {
		
		String strMaxDom = null;
		String strNewDom = null;
		ResultSet rsDomain = null;
		boolean bFoundBlank = false;
		
		strDomain = stripSquareBrackets(strDomain);
		
//		System.out.println("strDomain: "+strDomain);
		
		if(strDomain != null && !strDomain.contains(".")) {
			String strQuery = "SELECT chunkEnglish , chunkFilipino , domain"
				+" FROM Chunks WHERE domain LIKE '" + strDomain + ".%'";
	
			int nMaxLength = 0;
			Vector vDomains = new Vector();
			try{
				ResultSet rs = DBConnector.select(strQuery);
				while(rs.next() && !bFoundBlank) {
					String strDBDomain = rs.getString("domain");
					String strSChunk = rs.getString("chunkEnglish");
					String strTChunk = rs.getString("chunkFilipino");
					
					//check for blank domains
					if(strSChunk.equalsIgnoreCase(" ") &&
						strTChunk.equalsIgnoreCase(" ")) {
						bFoundBlank = true;
						strNewDom = strDBDomain;
					}else {
//						System.out.println("strDomain -->" + strDBDomain);
						
						if(strDBDomain == null)
							break;
						int nDomLength = strDBDomain.length();
						if(nDomLength > nMaxLength){
//							System.out.println(nDomLength + ">" + nMaxLength);
							nMaxLength = nDomLength;
							vDomains.clear();
							vDomains.add(strDBDomain);
//							System.out.println("vDomains-->" + vDomains);
						}
						else if(nDomLength == nMaxLength){
//							System.out.println(nDomLength + "==" + nMaxLength);
							vDomains.add(strDBDomain);
//							System.out.println("vDomains-->" + vDomains);
						}
//						else
//							System.out.println(nDomLength + "<" + nMaxLength);
					}

					strDBDomain = null;
					strSChunk = null;
					strTChunk = null;
				}
				
				rs = null;
			}catch(Exception sqle){
				System.out.println("There is an SQL Error!");
				sqle.printStackTrace();
			}
			
			if(!bFoundBlank) {
				int nMaxDomain = 0;
//				System.out.println("vDomains-->" + vDomains);
				for(Iterator it = vDomains.iterator(); it.hasNext(); ){
//					System.out.println("nMaxDomain -->" + nMaxDomain);
//					System.out.println("strMaxDomain -->" + strMaxDom);

					String strTemp = (String)it.next();

//					System.out.println("strTemp -->" + strTemp);

					int nTemp = Integer.parseInt(strTemp.substring(strTemp.indexOf(".")+1, strTemp.length()));
//					System.out.println("nTemp -->" + nTemp);
					if(nTemp > nMaxDomain){
//						System.out.println("nTemp > nMaxDomain");
						nMaxDomain = nTemp;
						strMaxDom = strTemp;
					}
//					else{
//						System.out.println("nTemp <= nMaxDomain");
//					}
					
					strTemp = null;
				}

				if(strMaxDom == null) {
					strNewDom = strDomain + ".1";
				}else {
					strNewDom = strDomain + "." + (Integer.parseInt(strMaxDom.substring(strMaxDom.indexOf(".") + 1, strMaxDom.length())) + 1);
				}
//				System.out.println("strMaxDom: "+strMaxDom);
			}
			
			strQuery = null;
			vDomains = null;

		}else {
			String strMaxInChunks = getMaxDomInChunks(vSrcChunks, vTarChunks,
										cSource, cTarget);
			String strMaxInDB = getMaxDomInDB();
			
//			System.out.println("strMaxInChunks: "+strMaxInChunks);
//			System.out.println("strMaxInDB: "+strMaxInDB);
			
			if(strMaxInChunks == null && strMaxInDB == null) {
				strNewDom = "1.1";
			}else if(strMaxInChunks == null && strMaxInDB != null) {
				strNewDom = (Integer.parseInt(strMaxInDB.substring(0, strMaxInDB.indexOf("."))) + 1) + ".1";
			}else if(strMaxInChunks != null && strMaxInDB == null) {
				strNewDom = (Integer.parseInt(strMaxInChunks.substring(0, strMaxInChunks.indexOf("."))) + 1) + ".1";
			}else {
				if((new Float(strMaxInChunks)).floatValue() >= (new Float(strMaxInDB)).floatValue()) {
					strNewDom = (Integer.parseInt(strMaxInChunks.substring(0, strMaxInChunks.indexOf("."))) + 1) + ".1";
				}else {
					strNewDom = (Integer.parseInt(strMaxInDB.substring(0, strMaxInDB.indexOf("."))) + 1) + ".1";
				}
			}
			
			strMaxInChunks = null;
			strMaxInDB = null;
		}

		int nDecimalPt = (new Integer (strNewDom.substring(strNewDom.indexOf(".") + 1,
			strNewDom.length()))).intValue();

//		System.out.println("strNewDom: "+strNewDom);
			
		if(strNewDom.contains(".") && nDecimalPt % 10 == 0) {
			strNewDom = strNewDom.substring(0, strNewDom.indexOf(".") + 1) + (nDecimalPt + 1);
		}

		strMaxDom = null;
		rsDomain = null;
		
		return strNewDom;
	}

	/**
	 *	? Will depend on addChunkToDB
	 *
	 *	This function ensures that the domain is of the form X.Y.
	 *	
	 *	This function is used when adding chunks in the DB.
	 *
	 */
	protected String checkDomain(String strDomain, Vector vSrcChunks,
		Vector vTarChunks, TExtChunk cSource, TExtChunk cTarget) {
		String strNew = getNewDomain(getWholeNoDomain(strDomain), vSrcChunks,
							vTarChunks, cSource, cTarget);
//		System.out.println("checkDomain: "+strNew);
		return strNew;
	}

	/**
	 *	Adds those which are in the Vector of AlignedChunk to the DB using
	 *	addChunkToDB()
	 *  vmod
	 */
	protected boolean addAlignedChunksToDB(Vector vAlignedOriginAndChunks,
		String strSource, String strTarget, Vector vSourceChunks, Vector vTargetChunks) {

		boolean bCheck = false;
		Vector vAlignedOrigin = null;
		Vector vAlignedChunks = null;
		Object oTmp = null;

		if(vAlignedOriginAndChunks != null && vAlignedOriginAndChunks.size() == 2) {
			vAlignedOrigin = (Vector)vAlignedOriginAndChunks.get(0);
			vAlignedChunks = (Vector)vAlignedOriginAndChunks.get(1);
			bCheck = true;

			for(int i = 0; i < vAlignedChunks.size() && bCheck; i++) {
				oTmp = vAlignedOrigin.get(i);
				
				if(oTmp instanceof AlignedChunk) {
					bCheck = addChunkToDB(((AlignedChunk)vAlignedChunks.get(i)).getSource(), 
						((AlignedChunk)vAlignedChunks.get(i)).getTarget(),
						strSource, strTarget,
						((AlignedChunk)oTmp).getSource(),
						((AlignedChunk)oTmp).getTarget(),
						vSourceChunks, vTargetChunks);
					/* try modify parent here.
					 * modify parent should generalize parent: 
					 * [5/3.3/3.2], 5 should be removed since no child of 5 is in domain
					 * duplicates?
					 */					
					modifyParentDomain(  ((AlignedChunk)oTmp).getSource(),((AlignedChunk)oTmp).getTarget(),"","");
					
				}else {
					bCheck = addChunkToDB(((AlignedChunk)vAlignedChunks.get(i)).getSource(), 
						((AlignedChunk)vAlignedChunks.get(i)).getTarget(),
						strSource, strTarget,
						((AlignedTemplate)oTmp).getSource(),
						((AlignedTemplate)oTmp).getTarget(),
						vSourceChunks, vTargetChunks);
					/* try modify parent here.
					 * modify parent should generalize parent: 
					 * [5/3.3/3.2], 5 should be removed since no child of 5 is in domain
					 * duplicates?
					 */					
					modifyParentDomain(  ((AlignedTemplate)oTmp).getSource(),((AlignedTemplate)oTmp).getTarget(),"","");
				}
				
				oTmp = null;
			}
			vAlignedOrigin = null;
			vAlignedChunks = null;
		}

		return bCheck;
	}
	
	
	

	/*
	 *	This function adds the source and target chunk to the DB & ensures no 
	 *	duplication
	 *	
	 *	not tested
	 * Object oSOrigin, Object oTOrigin: TExtTemplate or TExtChunk
	 * @param
	 **/
	
	
	/**
	 * @param cSrcChunk the source chunk
	 * @param cTarChunk
	 * @param strSource
	 * @param strTarget
	 * @param oSOrigin
	 * @param oTOrigin
	 * @param vSourceChunks used only in checking for duplicates
	 * @param vTargetChunks used only in checking for duplicates
	 * 
	 */
	protected boolean addChunkToDB(TExtChunk cSrcChunk, TExtChunk cTarChunk,
		String strSource, String strTarget, Object oSOrigin, Object oTOrigin,
		Vector vSourceChunks, Vector vTargetChunks) {

		// should i pass parent to startRefinement
		 
		boolean bRefinementDone=chunkRefinement( new AlignedChunk(cSrcChunk, cTarChunk),(Vector) oSOrigin, (Vector)oTOrigin, null);
		if (bRefinementDone) {
			// mod parent based on certain unknown conditions
			// actually, if TR, mod parent inside deriveTemplate() algo selection TR section
			
			//oSOrigin and oTOrigin has already been written, pwede ko na to patungan
			//the parents will be modified inside Refinement. succeeding calls will
			//be wrong if they are not updated.
			/*
			Vector vSOrigin = (Vector) oSOrigin;
			Vector vTOrigin = (Vector) oTOrigin;
			
			((Vector)oSOrigin).clear();
			((Vector)oTOrigin).clear();
			
			((Vector)oSOrigin).addAll(vSOrigin);
			((Vector)oTOrigin).addAll(vTOrigin);
			*/
			return true;
		}
		
		
		
		boolean bCheck = true;
		String strDomain = cSrcChunk.domain();
		ResultSet rsDuplicate = null;
	
		//ensures: chunks to be added are not empty
		if(cSrcChunk.size() > 0 && cTarChunk.size() > 0) {
			if(strDomain.contains(".")) {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = '" 
					+ cSrcChunk.toString().replace("'","''") + "'"
					+ " AND chunk" + strTarget + " = '" 
					+ cTarChunk.toString().replace("'","''")+ "'"
					+ " AND domain LIKE '"
					+ strDomain.substring(0,strDomain.indexOf('.')) + ".%'"
					+ " AND compID" + strSource + " = '"
					+ cSrcChunk.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '"
					+ cTarChunk.getCompoundIdentifier() + "'");
			}else {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = '" 
					+ cSrcChunk.toString().replace("'","''") + "'"
					+ " AND chunk" + strTarget + " = '" 
					+ cTarChunk.toString().replace("'","''")+ "'"
					+ " AND domain LIKE '"
					+ strDomain + ".%'"
					+ " AND compID" + strSource + " = '"
					+ cSrcChunk.getCompoundIdentifier() + "'"
					+ " AND compID" + strTarget + " = '"
					+ cTarChunk.getCompoundIdentifier() + "'");
			}
			
			try {
				//check for duplicates && source domain == target domain
				if(!rsDuplicate.next() && 
					cSrcChunk.domain().equalsIgnoreCase(cTarChunk.domain()) &&
					!checkDuplicateChunk(cSrcChunk, cTarChunk,
					strSource, strTarget, oSOrigin, oTOrigin,
					vSourceChunks, vTargetChunks) &&
					!checkBlankInDomain(cSrcChunk, cTarChunk,
					strSource, strTarget, vSourceChunks, vTargetChunks)) {
					
					int nCnstCntS = 0, nCnstCntT = 0;
					
					for(Iterator it = cSrcChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntS++;
					for(Iterator it = cTarChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntT++;
					
					String strUpdate = "INSERT INTO Chunks"
						+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
						+ "compID" + strSource + ", compID" + strTarget + ", "
						+ "constCnt" + strSource.substring(0, 3) + ", "
						+ "constCnt" + strTarget.substring(0, 3) + ") "
						+ "VALUES ('" + cSrcChunk.toString().replace("'","''")
						+ "', '"
						+ cTarChunk.toString().replace("'","''")
						+ "', '"
						+ strDomain
						+ "', '" 
						+ cSrcChunk.getCompoundIdentifier()
						+ "', '" 
						+ cTarChunk.getCompoundIdentifier()
						+ "', " 
						+ nCnstCntS + ", " + nCnstCntT + ")";
						
					bCheck = DBConnector.update(strUpdate);
					

					
					AlignedChunk acChunk = new AlignedChunk(cSrcChunk, cTarChunk);
					
					vTRChunks.add(acChunk);
//					System.out.println("Inserting: SUCCESSFUL!");
				}else {
					//No need to add the chunk, it's already in the DB
//					System.out.println("Inserting: NO NEED!");
					
//--------------->>>check if all doms of the templates are in the db
					//checkDBDomainExistence(oSOrigin, oTOrigin, strSource, strTarget);
				}
			} catch(SQLException sqle){
				System.out.println("There was an error while inserting the"
				+" chunks from refinement in the database.");
				bCheck = false;
			}
		}
		
		strDomain = null;
		rsDuplicate = null;

		return bCheck;
	}

	/*
	 *	This fcn checks if the domain exists in the chunks db
	 */
	protected void checkDBDomainExistence(Object oSOrigin, Object oTOrigin,
		String strSource, String strTarget) {
		
		boolean bCheck;
//System.out.println("check if all doms of the templates are in the db");

		Object oTmp = null;
		String strTmpDom = null;
		String[] arrStrDomains = null;
		ResultSet rsDomain = null;

		if(oSOrigin instanceof TExtTemplate) {
			for(int i = 0; i < ((TExtTemplate)oSOrigin).size(); i++) {
				oTmp = ((TExtTemplate)oSOrigin).get(i);
				if(oTmp instanceof TExtChunk) {
					arrStrDomains = stripSquareBrackets(((TExtChunk)oTmp).toString()).split("/");
					//check each variable if it contains at least one entry in db
					for(int j = 0; j < arrStrDomains.length; j++) {
						
						if(arrStrDomains[j].contains(".")) {
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '"+arrStrDomains[j]+"%'");
						}else{
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '" + arrStrDomains[j] + ".%'");
						}
				
						try {
							//if one is not found, add a blank in that variable
							if(!rsDomain.next()){
								if(!arrStrDomains[j].contains("."))
									arrStrDomains[j] += ".1";
									
								String strUpdate = "INSERT INTO Chunks"
									+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
									+ "compID" + strSource + ", compID" + strTarget + ", "
									+ "constCnt" + strSource.substring(0, 3) + ", "
									+ "constCnt" + strTarget.substring(0, 3) + ") "
									+ "VALUES (' ', ' ', '"
									+ arrStrDomains[j]
									+ "', 'none', 'none', 0, 0)";
								
								//bCheck = DBConnector.update(strUpdate);
								bCheck = true;
							}
						} catch(SQLException sqle){
							System.out.println("There is an SQL Error!");
							sqle.printStackTrace();
						}
					}
				}
			}
		}else if(oSOrigin instanceof TExtChunk) {
			for(int i = 0; i < ((TExtChunk)oSOrigin).size(); i++) {
				oTmp = ((TExtChunk)oSOrigin).get(i);
				if(oTmp instanceof TExtChunk) {
					arrStrDomains = stripSquareBrackets(((TExtChunk)oTmp).toString()).split("/");
					//check each variable if it contains at least one entry in db
					for(int j = 0; j < arrStrDomains.length; j++) {
						
						if(arrStrDomains[j].contains(".")) {
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '"+arrStrDomains[j]+"%'");
						}else{
							rsDomain = DBConnector.select("SELECT chunk"+
								strSource+" , chunk"+strTarget+" , domain , compID"+strSource+
								" , compID"+strTarget+" FROM Chunks WHERE domain LIKE '" + arrStrDomains[j] + ".%'");
						}
				
						try {
							//if one is not found, add a blank in that variable
							if(!rsDomain.next()){
								
								if(!arrStrDomains[j].contains("."))
									arrStrDomains[j] += ".1";
								
								
								
								//HOYHOYHOYHOY	
								String strUpdate = "INSERT INTO Chunks"
									+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
									+ "compID" + strSource + ", compID" + strTarget + ", "
									+ "constCnt" + strSource.substring(0, 3) + ", "
									+ "constCnt" + strTarget.substring(0, 3) + ") "
									+ "VALUES (' ', ' ', '"
									+ arrStrDomains[j]
									+ "', 'none', 'none', 0, 0)";
								
							//	bCheck = DBConnector.update(strUpdate);
								bCheck = true;
							}
						} catch(SQLException sqle){
							System.out.println("There is an SQL Error!");
							sqle.printStackTrace();
						}
					}
				}
			}
		}
	}

	/**
	 *	This function check for blanks in the domain.
	 *
	 *	if there's blank,
	 *		use update
	 *		return true
	 *	else
	 *		return false
	 *
	 */
	protected boolean checkBlankInDomain(TExtChunk cSrcChunk,
		TExtChunk cTarChunk, String strSource, String strTarget,
		Vector vSourceChunks, Vector vTargetChunks) {
			
		boolean bCheck = false;
		String strDomain = cSrcChunk.domain();
		ResultSet rsDuplicate = null;
		
		if(cSrcChunk.size() > 0 && cTarChunk.size() > 0) {
			if(strDomain.contains(".")) {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = ' '"
					+ " AND chunk" + strTarget + " = ' '"
					+ " AND domain LIKE '" + strDomain + "%'");
			}else {
				rsDuplicate = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
					+ "chunk" + strSource + " = ' '"
					+ " AND chunk" + strTarget + " = ' '"
					+ " AND domain LIKE '" + strDomain + ".%'");
			}
			
			try {
				if(rsDuplicate.next()) {
					
					int nCnstCntS = 0, nCnstCntT = 0;
					
					for(Iterator it = cSrcChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntS++;
					for(Iterator it = cTarChunk.iterator(); it.hasNext(); )
						if(it.next() instanceof String)
							nCnstCntT++;
					
					bCheck = DBConnector.update("UPDATE Chunks SET "
						+ "chunk" + strSource + " = '" + cSrcChunk.toString().replace("'","''") + "' , "
						+ "chunk" + strTarget + " = '" + cTarChunk.toString().replace("'","''") + "' , "
						+ "domain = '" 
						+ cSrcChunk.domain()
						+ "' , "
						+ "compID" + strSource + " = '" + cSrcChunk.getCompoundIdentifier() + "' , "
						+ "compID" + strTarget + " = '" + cTarChunk.getCompoundIdentifier() + "' , "
						+ "constCnt" + strSource.substring(0, 3) + " = '" + nCnstCntS + "' , "
						+ "constCnt" + strTarget.substring(0, 3) + " = '" + nCnstCntT + "' "
						+ "WHERE chunkID = '" + rsDuplicate.getString("chunkID") + "'");

//					System.out.println("Inserting: SUCCESSFUL!");
				}
			} catch(SQLException sqle){
				System.out.println("There was an error while inserting the"
				+" chunks from refinement in the database.");
			}
		}
		
		return bCheck;
	}

	/**
	 *	This function checks for duplicate chunks in the DB with a different
	 *	domain.
	 *
	 *	oSOrigin and oTOrigin can either be a template or a chunk assumes that
	 *	cSrcChunk and cTarChunk contains the original domain meaning that
	 *	domain is also the domain to be replaced in the oSOrigin and oTOrigin
	 *	
	 *	not tested	
	 */
	protected boolean checkDuplicateChunk(TExtChunk cSrcChunk, TExtChunk cTarChunk,
		String strSource, String strTarget, Object oSOrigin, Object oTOrigin,
		Vector vSourceChunks, Vector vTargetChunks) {
			
		boolean bDuplicate = false, bCheck = false;

//		System.out.println("*****UPDATE DUPLICATE CHUNKS*****");

		ResultSet rsDuplicate = DBConnector.select("SELECT domain FROM Chunks WHERE "
			+ "chunk" + strSource + " = '" 
			+ cSrcChunk.toString().replace("'","''") + "'"
			+ " AND chunk" + strTarget + " = '" 
			+ cTarChunk.toString().replace("'","''")+ "'"
			+ " AND compID" + strSource + " = '"
			+ cSrcChunk.getCompoundIdentifier() + "'"
			+ " AND compID" + strTarget + " = '"
			+ cTarChunk.getCompoundIdentifier() + "'");
		String strDomain = null;
		Object oTmp = null;
		ResultSet rsDuplicateBlank = null;
					
		try {
			//check for duplicate chunk
			while(rsDuplicate.next()) {
				bDuplicate = true;
				strDomain = rsDuplicate.getFloat("domain") + "";

				if(oSOrigin instanceof TExtTemplate && oTOrigin instanceof TExtTemplate) {
					//START edit the template of the origin
					//get the appropriate replacement for the template, usu. a slash is added
					StringBuffer strbSNewTemplate = new StringBuffer();
					StringBuffer strbTNewTemplate = new StringBuffer();
					String strSWhole = cSrcChunk.domain().substring(0, cSrcChunk.domain().indexOf("."));
					String strTWhole = cTarChunk.domain().substring(0, cTarChunk.domain().indexOf("."));

					for(int i = 0; i < ((TExtTemplate)oSOrigin).size() 
						|| i < ((TExtTemplate)oTOrigin).size(); i++) {
						if(i < ((TExtTemplate)oSOrigin).size()) {
							oTmp = ((TExtTemplate)oSOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
//								System.out.println("oTmp: "+oTmp);
								//no need to add the X.Y because it is already in the template
								//where X.Y is the duplicate chunk
								//or X of X.Y
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString());
									
								}
								//if there's Z where Z is taken from Z.A and if it's already in db
								//no need to add
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString());

								}
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString());

								}
								//Z.A should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewTemplate.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));

								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
										
									strbSNewTemplate.append(((TExtChunk)oTmp).toString().replace(strSWhole,
										strSWhole+"/"+strDomain));
//									strbSNewTemplate.append(((TExtChunk)oTmp).toString());

								}else {
									strbSNewTemplate.append(oTmp);

								}
							}else {
								strbSNewTemplate.append(oTmp);
							}
						}
						
						if(i < ((TExtTemplate)oTOrigin).size()) {
							oTmp = ((TExtTemplate)oTOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
								//no need to add the X.Y because it is already in the template
								//where X.Y is the duplicate chunk
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}
								//Z should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewTemplate.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));
								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									strbTNewTemplate.append(((TExtChunk)oTmp).toString().replace(strTWhole,
										strTWhole+"/"+strDomain));
//									strbTNewTemplate.append(((TExtChunk)oTmp).toString());
								}else {
									strbTNewTemplate.append(oTmp);
								}
							}else {
								strbTNewTemplate.append(oTmp);
							}
						}
						
						if(i < ((TExtTemplate)oSOrigin).size() - 1) {
							strbSNewTemplate.append(" ");
						}
						
						if(i < ((TExtTemplate)oTOrigin).size() - 1) {
							strbTNewTemplate.append(" ");
						}
					}
					//END edit the template of the origin

					//update the template of the origin
					/* v note: this is safe. this is not called because of "if" .
					 * this function works on Chunks and Templates. uses instanceof.
					 */
					String strOriginS =  ((TExtTemplate)oSOrigin).toString();
					String strOriginT =  ((TExtTemplate)oTOrigin).toString();
					modifyParentDomainTR(oSOrigin, oTOrigin, cSrcChunk.domain(), strDomain );			
					bCheck = true;
					/*
					String strUpdate = "UPDATE Templates SET "
						+ "template" + strSource + " = '" + strbSNewTemplate.toString().replace("'","''") + "' , "
						+ "template" + strTarget + " = '" + strbTNewTemplate.toString().replace("'","''") + "' "
						+ "WHERE template" + strSource + " = '"
						+ ((TExtTemplate)oSOrigin).toString() + "' AND "
						+ "template" + strTarget + " = '"
						+ ((TExtTemplate)oTOrigin).toString() + "' AND "
						+ "compID" + strSource + " = '"
						+ ((TExtTemplate)oSOrigin).getCompoundIdentifier() + "' AND "
						+ "compID" + strTarget + " = '"
						+ ((TExtTemplate)oTOrigin).getCompoundIdentifier() + "'";
					
					bCheck = DBConnector.update(strUpdate);
					*/
					
					
					TExtTemplate tNewSource = new TExtTemplate(strbSNewTemplate.toString().trim(),
								((TExtTemplate)oSOrigin).getCompoundIdentifier());
					TExtTemplate tNewTarget = new TExtTemplate(strbTNewTemplate.toString().trim(),
								((TExtTemplate)oTOrigin).getCompoundIdentifier());
		
					AlignedTemplate atNewRefinement = new AlignedTemplate(tNewSource, tNewTarget);
					if(vTRTemplates.size() > 0) {
						vTRTemplates.removeElementAt(vTRTemplates.size()-1);
					}
					vTRTemplates.add(atNewRefinement);
		
					//START adding blanks - ensures no multiple blanks
					StringBuffer strbChunkDom = new StringBuffer(cSrcChunk.domain());
					boolean bBlankDuplicate = false;
					boolean bWholeDomExists = false;
					
					if(strbChunkDom.toString().contains(".")) {
						strbChunkDom = new StringBuffer(strbChunkDom.substring(0, strbChunkDom.indexOf(".")));
					}
					
					rsDuplicateBlank = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+ "chunk" + strSource + " = ' '"
								+ " AND chunk" + strTarget + " = ' '"
								+ " AND domain LIKE '" + strbChunkDom.toString() + ".%'"
								+ " AND compID" + strSource + " = 'none'"
								+ " AND compID" + strTarget + " = 'none'");
				
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsDuplicateBlank.next()) {
							bBlankDuplicate = true;
//							System.out.println("----->>>duplicate blank!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					ResultSet rsWholeDomExists = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+ "domain LIKE '" + strbChunkDom.toString() + ".%'");
					
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsWholeDomExists.next()) {
							bWholeDomExists = true;
//							System.out.println("----->>>WholeDomExists!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					if(!bBlankDuplicate && !bWholeDomExists) {
						
						//v added.. check din ung baba. ganito rin ginawa ko.
						String strCS = oSOrigin.toString();
						String strCT = oTOrigin.toString();
						modifyParentDomainTR(oSOrigin,oTOrigin,cSrcChunk.domain(), strDomain);
						bCheck = true;
						
						String strUpdate2 = "INSERT INTO Chunks"
							+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
							+ "constCnt" + strSource.substring(0, 3) + ", constCnt" + strTarget.substring(0, 3)
							+ ") VALUES (' ', ' ', '" 
							+ cSrcChunk.domain()
							+ "', "
							+ "0, 0)";
						//v comment.. dont add blank
						//bCheck = DBConnector.update(strUpdate2);
					}
					//END adding blanks - ensures no multiple blanks
					strbSNewTemplate = null;
					strbTNewTemplate = null;
					strSWhole = null;
					strTWhole = null;
					strbChunkDom = null;
				}
				//assumes: that oSOrigin and oTOrigin is a TExtChunk
				else {
					//START edit the chunk of the origin
					//get the appropriate replacement for the chunk, usu. a slash is added
					StringBuffer strbSNewChunk = new StringBuffer();
					StringBuffer strbTNewChunk = new StringBuffer();
					String strSWhole = cSrcChunk.domain().substring(0, cSrcChunk.domain().indexOf("."));
					String strTWhole = cTarChunk.domain().substring(0, cTarChunk.domain().indexOf("."));

					for(int i = 0; i < ((TExtChunk)oSOrigin).size() 
						|| i < ((TExtChunk)oTOrigin).size(); i++) {
						if(i < ((TExtChunk)oSOrigin).size()) {
							oTmp = ((TExtChunk)oSOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
								//no need to add the X.Y because it is already in the chunk
								//where X.Y is the duplicate chunk
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}
								//no need to add
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}
								//Z should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cSrcChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbSNewChunk.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));
								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strSWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
										strbSNewChunk.append(((TExtChunk)oTmp).toString().replace(strSWhole,
										strSWhole+"/"+strDomain));
//									strbSNewChunk.append(((TExtChunk)oTmp).toString());
								}else {
									strbSNewChunk.append(oTmp);
								}
							}else {
								strbSNewChunk.append(oTmp);
							}
						}
						
						if(i < ((TExtChunk)oTOrigin).size()) {
							oTmp = ((TExtChunk)oTOrigin).get(i);
							if(oTmp instanceof TExtChunk) {
								//no need to add the X.Y because it is already in the chunk
								//where X.Y is the duplicate chunk
								if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*/"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strDomain
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}
								//Z should be replaced with Z/X.Y where X.Y is the duplicate chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+cTarChunk.domain()
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
									
									strbTNewChunk.append(((TExtChunk)oTmp).toString().replace(cSrcChunk.domain(),
										cSrcChunk.domain()+"/"+strDomain));
								}
								//if there's Z where Z is taken from Z.A then no 
								//need to put Z.A instead put the prev chunk
								else if(((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]") ||
									((TExtChunk)oTmp).toString().matches("\\[([0-9]+(\\.[0-9]+)?/)*"
									+strTWhole
									+"(/[0-9]+(\\.[0-9]+)?)*\\]")) {
										strbTNewChunk.append(((TExtChunk)oTmp).toString().replace(strTWhole,
										strTWhole+"/"+strDomain));
//									strbTNewChunk.append(((TExtChunk)oTmp).toString());
								}else {
									strbTNewChunk.append(oTmp);
								}
							}else {
								strbTNewChunk.append(oTmp);
							}
						}
						
						if(i < ((TExtChunk)oSOrigin).size() - 1) {
							strbSNewChunk.append(" ");
						}
						
						if(i < ((TExtChunk)oTOrigin).size() - 1) {
							strbTNewChunk.append(" ");
						}
					}
					//END edit the template of the origin
					
					//need to generalize the domain bcuz [109/94.1] is being written, there is no 109.
					// 94.1 is the duplicate
					String strOriginS =  ((TExtChunk)oSOrigin).toString();
					String strOriginT =  ((TExtChunk)oTOrigin).toString();
					//dapat yata hindi ako mag modify dito... kasi nabubura ko yung reference. pag ginamit nung next child wala na ung reference
					modifyParentDomainTR(oSOrigin, oTOrigin, cSrcChunk.domain(), strDomain );
					bCheck = true;
					//update the chunk of the origin
					/*
					
					bCheck = DBConnector.update("UPDATE Chunks SET "
						+ "chunk" + strSource + " = '" + strbSNewChunk.toString().replace("'","''") + "' , "
						+ "chunk" + strTarget + " = '" + strbTNewChunk.toString().replace("'","''") + "' "
						+ "WHERE chunk" + strSource + " = '" 
						+ strOriginS + "' AND "
						+ "chunk" + strTarget + " = '"
						+ strOriginT + "' AND "
						+ "compID" + strSource + " = '"
						+ ((TExtChunk)oSOrigin).getCompoundIdentifier() + "' AND "
						+ "compID" + strTarget + " = '"
						+ ((TExtChunk)oTOrigin).getCompoundIdentifier() + "'");
					
					*/
					TExtChunk cS = new TExtChunk(strbSNewChunk.toString(), "999",((TExtChunk)oSOrigin).getCompoundIdentifier() );
					TExtChunk cT = new TExtChunk(strbTNewChunk.toString(), "999",((TExtChunk)oTOrigin).getCompoundIdentifier() );
					cS.adjustChunkCompound();
					cT.adjustChunkCompound();
					
					((TExtChunk)oSOrigin).clear();
					((TExtChunk)oTOrigin).clear();
					
					((TExtChunk)oSOrigin).addAll(cS);
					((TExtChunk)oTOrigin).addAll(cT);
					
					
					//START adding blanks - ensures no multiple blanks
					StringBuffer strbChunkDom = new StringBuffer(cSrcChunk.domain());
					boolean bBlankDuplicate = false;
					boolean bWholeDomExists = false;
					
					if(strbChunkDom.toString().contains(".")) {
						strbChunkDom = new StringBuffer(strbChunkDom.substring(0, strbChunkDom.indexOf(".")));
					}
					
					rsDuplicateBlank = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+ "chunk" + strSource + " = ' '"
								+ " AND chunk" + strTarget + " = ' '"
								+ " AND domain LIKE '" + strbChunkDom.toString() + ".%'"
								+ " AND compID" + strSource + " = 'none'"
								+ " AND compID" + strTarget + " = 'none'");
				
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsDuplicateBlank.next()) {
							bBlankDuplicate = true;
//							System.out.println("----->>>duplicate blank!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					ResultSet rsWholeDomExists = DBConnector.select("SELECT chunkID FROM Chunks WHERE "
								+"domain LIKE '" + strbChunkDom.toString() + ".%'");
				
//					System.out.println("strbChunkDom: "+strbChunkDom.toString());
					
					try {
						//check for duplicate blank with the same domain
						while(rsWholeDomExists.next()) {
							bWholeDomExists = true;
//							System.out.println("----->>>WholeDomExists!!!");
						}
					} catch(SQLException e){
						e.printStackTrace();
						//System.out.println("There was an error while updating the chunks from refinement in the database.");
						bCheck = false;
					}
					
					if(!bBlankDuplicate && !bWholeDomExists && false ) {
						String strCS = oSOrigin.toString();
						String strCT = oTOrigin.toString();
						modifyParentDomain(oSOrigin,oTOrigin,cSrcChunk.domain(), strDomain);
						bCheck = true;
						
						String strUpdate = "INSERT INTO Chunks"
							+ " (chunk" + strSource + ", chunk" + strTarget + ", domain, "
							+ "constCnt" + strSource.substring(0, 3) + ", constCnt" + strTarget.substring(0, 3)
							+ ") VALUES (' ', ' ', '" 
							+ cSrcChunk.domain()
							+ "', "
							+ "0, 0)";
						//v comment.. do not add blank
						//bCheck = DBConnector.update(strUpdate);
					}
					//END adding blanks - ensures no multiple blanks
					strbSNewChunk = null;
					strbTNewChunk = null;
					strSWhole = null;
					strTWhole = null;
					strbChunkDom = null;
				}
				strDomain = null;
			}
		} catch(SQLException sqle){
			sqle.printStackTrace();
			bCheck = false;
		}
		
//		System.out.println("bCheck: "+bCheck);
//		System.out.println("bDuplicate: "+bDuplicate);
//		System.out.println("*****END UPDATE DUPLICATE CHUNKS*****");

		rsDuplicate = null;
		
		return bDuplicate;
	}
	
//MIN END
	
	
	private void println(String x){
		System.out.println(x);
	}
	
	
	
	
	
	/**
	 * from the similarity match, 
	 * 
	 * if it is not in sttlalignment, add it.
	 * if it is in exclude, add if it is not in sttlalignment
	 * if it is not in exclude, add

	 * @param vMatches the similarity matches
	 * @param vTokens the tokens of the template
	 * @param vExclude exclude list
	 * @param hSTTLAlignment alignment
	 * @return
	 */
	private Vector getDifferenceMatches(Vector vTokens, Hashtable hExclude, Hashtable hSTTLAlignment){
		Vector vDiffMatch = new Vector();
		Hashtable hash2  = (Hashtable)hSTTLAlignment.clone();

		
		for(int i=0; i < vTokens.size(); i++){
			String o =(String) vTokens.get(i);
			boolean x = false;
			Integer nExcludeIndex = (Integer)hExclude.get(o);
				
				//if it is in exclude list, add.
				if (nExcludeIndex != null && i == nExcludeIndex.intValue() ){
					Vector vhashval =(Vector) ((Vector)hash2.get(o)).clone();
					if (vhashval!= null && !vhashval.isEmpty() &&vhashval.get(0) == null) { 
						// i need the hash of include list, temporary lang to
						// if it is not in include list, add
						vDiffMatch.add(o);
						hash2.remove(o);
						continue;
					}else if (vhashval!= null && vhashval.get(0) != null){	
						//((Vector)hash2.get(o)).remove(0); //hash2 talaga to.
						continue;
					}
					else vDiffMatch.add(o);
				}else{
					vDiffMatch.add(o);
				}
		}	
		
		return vDiffMatch;
	}
	
	
	
	
	
	
	
	protected boolean deriveChunkSimDiff(AlignedChunk alChunk, AlignedChunk alMatch) {
		//System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
		TExtChunk cTempS = alChunk.getSource(), cTempT = alChunk.getTarget();
		TExtChunk cMatchS, cMatchT;
		
		boolean bReturnSuccess = false; //v add, returns true of STTL is performed
		
		cMatchS = alMatch.getSource();
		cMatchT = alMatch.getTarget();
			
		String strDomain = alMatch.getSource().domain(); //v added this.
		
		String strTempCompIDS = cTempS.getCompoundIdentifier();
		String strTempCompIDT = cTempT.getCompoundIdentifier();

		Vector vTempTokenS = (Vector)cTempS.clone(),
			vTempTokenT = (Vector)cTempT.clone(),
			vMatchTokenS = (Vector)cMatchS.clone(),
			vMatchTokenT = (Vector)cMatchT.clone();

		
		Vector vSal = new Vector();
		Vector vTal = new Vector();
		vSal.add(alChunk.getSource());
		vTal.add(alChunk.getTarget());
		Hashtable hAlignment = (Hashtable) ((Vector)getAlignmentInChunks( vSal , vTal )).get(2);
		
		vSal.clear(); vSal.add(alMatch.getSource());
		vTal.clear(); vTal.add(alMatch.getTarget());
		Hashtable hMatchAlignment = (Hashtable) ((Vector)getAlignmentInChunks( vSal , vTal )).get(2); 
		
		Vector vMatchesS = new Vector(1), vMatchesT = new Vector(1);
		 
		Hashtable hSTTLAlignmentS = new Hashtable(1);
		Hashtable hSTTLAlignmentT = new Hashtable(1);
		
		Vector vExcludeS = new Vector(1);
		Vector vExcludeT = new Vector(1);
		
		Hashtable hExcludeS1 = new Hashtable(1);
		Hashtable hExcludeS2 = new Hashtable(1);
		Hashtable hExcludeT1 = new Hashtable(1);
		Hashtable hExcludeT2 = new Hashtable(1);
		
		boolean bDTTL = false;
		
		for(int nDTTL=0; nDTTL<2; nDTTL++){
			// SOURCE
			vMatchesS = getStringMatches((Vector)vTempTokenS.clone(), 
				(Vector)vMatchTokenS.clone());
			
			Vector vDiffMatchS1 = new Vector(1); //for lunch
			Vector vDiffMatchS2 = new Vector(1); // for dinner

			//testing for differences
			if(bDTTL)
				vDiffMatchS1 = getDifferenceMatches(vTempTokenS, hExcludeS1, hSTTLAlignmentS);		
			else vDiffMatchS1 = vMatchesS;
			
			if(bDTTL) 
				vDiffMatchS2 = getDifferenceMatches(vMatchTokenS, hExcludeS2, hSTTLAlignmentS);
			else vDiffMatchS2 = vMatchesS;
			
			//end testing
			
			System.out.println(vMatchesS);

			//FOR COMMON WORDS
			if(tlu.computeSpecificity(vDiffMatchS1, true) == 0)
				return bReturnSuccess;
			//END FOR COMMON WORDS
		
			//to avoid learning a template with all constants
			if(vTempTokenS.size() == vDiffMatchS1.size() || vDiffMatchS1.size() == 0)
				return bReturnSuccess;
			//end
		
			//System.out.println("Checkpoint 0.5");
			// TARGET
			vMatchesT = getStringMatches((Vector)vTempTokenT.clone(), 
				(Vector)vMatchTokenT.clone());

			Vector vDiffMatchT1 = new Vector(); 
			Vector vDiffMatchT2 = new Vector();
			
			if(bDTTL)
				vDiffMatchT1 = getDifferenceMatches(vTempTokenT, hExcludeT1, hSTTLAlignmentT);
			else vDiffMatchT1 = vMatchesT;
			
			if(bDTTL) 
				vDiffMatchT2 = getDifferenceMatches(vMatchTokenT, hExcludeT2, hSTTLAlignmentT);
			else vDiffMatchT2 = vMatchesT;
			
			
			//System.out.println(vMatchesT);

	//FOR COMMON WORDS
			if(tlu.computeSpecificity(vDiffMatchT1, false) == 0)
				return bReturnSuccess;
			
			if(tlu.computeSpecificity(vDiffMatchT2, false) == 0)
				return bReturnSuccess;
	//END FOR COMMON WORDS

			//to avoid learning a template with all constants
			if(vTempTokenT.size() == vDiffMatchT1.size() || vDiffMatchT1.size() == 0)
				return bReturnSuccess;
			//end
			 
			Vector vResult = new Vector(1);
			// SOURCE
			vResult = constructPattern((Vector)vTempTokenS.clone(), 
				(Vector)vDiffMatchS1.clone());
			Vector vTemplateTempS = (Vector)vResult.get(0);
			int nTempSChunkCnt = ((Integer)vResult.get(1)).intValue();
			
			//System.out.println(".......................");
			
			// TARGET
			vResult = constructPattern((Vector)vTempTokenT.clone(), 
				(Vector)vDiffMatchT1.clone());
			Vector vTemplateTempT = (Vector)vResult.get(0);
			int nTempTChunkCnt = ((Integer)vResult.get(1)).intValue();
			
			
			// SOURCE
			int nMatchSChunkCnt;
			Vector vTemplateTempS2;
			if (bDTTL){
				vResult = constructPattern((Vector)vMatchTokenS.clone(), 
						(Vector)vDiffMatchS2.clone());
				vTemplateTempS2 = (Vector)vResult.get(0);
				nMatchSChunkCnt = ((Integer)vResult.get(1)).intValue();
				
			}else{
				vResult = checkPattern((Vector)vMatchTokenS.clone(),
						(Vector)vDiffMatchS2.clone(), (Vector)vTemplateTempS.clone());
					
				if(!((Boolean)vResult.get(0)).booleanValue() && !bDTTL)
						return bReturnSuccess;
				else
					nMatchSChunkCnt = ((Integer)vResult.get(1)).intValue();
			}

			
			// TARGET
			int nMatchTChunkCnt;
			Vector vTemplateTempT2;
			if(bDTTL){
				vResult = constructPattern((Vector)vMatchTokenT.clone(), 
						(Vector)vDiffMatchT2.clone());
				vTemplateTempT2 = (Vector)vResult.get(0);
				nMatchTChunkCnt = ((Integer)vResult.get(1)).intValue();
				
			}else{
				vResult = checkPattern((Vector)vMatchTokenT.clone(),
						(Vector)vDiffMatchT2.clone(), (Vector)vTemplateTempT.clone());
				if(!((Boolean)vResult.get(0)).booleanValue() && !bDTTL)
					return bReturnSuccess;
				else
					nMatchTChunkCnt = ((Integer)vResult.get(1)).intValue();
			}
			
			if(nTempSChunkCnt != nMatchSChunkCnt || nTempTChunkCnt != nMatchTChunkCnt)
				return bReturnSuccess;

	//FOR VALIDITY OF CONSTANTS		
			
				if(!tlu.isValidSimilarities((Vector)vDiffMatchS1.clone(), (Vector)vDiffMatchT1.clone(), hAlignment))
				return bReturnSuccess;
	//END FOR VALIDITY OF CONSTANTS

				
				// SOURCE TEMP lunch here
				vResult = constructTemplateStringAndChunks((Vector)vTempTokenS.clone(),
					(Vector)vDiffMatchS1.clone(), true, strTempCompIDS, true);
				StringBuffer sbTempS1 = (StringBuffer)vResult.get(0);
				Vector vSourceChunks1 = (Vector)vResult.get(1);
				Vector vRemoved = (Vector)vResult.get(2);
				
				Vector vRemoved2 = tlu.editCompID(strTempCompIDS, vRemoved);
				strTempCompIDS = tlu.adjustCompIDVal(tlu.removeCompID(strTempCompIDS, vRemoved2), vRemoved);
				
			
				// SOURCE MATCH dinner here
				vResult = constructTemplateStringAndChunks((Vector)vMatchTokenS.clone(),
					(Vector)vDiffMatchS2.clone(), false, null, true);
				StringBuffer sbTempS2 = (StringBuffer)vResult.get(0);
				Vector vSourceChunks2 = (Vector)vResult.get(1);
				
				
				// TARGET TEMP
				vResult = constructTemplateStringAndChunks((Vector)vTempTokenT.clone(),
					(Vector)vDiffMatchT1.clone(), true, strTempCompIDT, false);
				StringBuffer sbTempT1 = (StringBuffer)vResult.get(0);
				Vector vTargetChunks1 = (Vector)vResult.get(1);
				vRemoved = (Vector)vResult.get(2);
				
				vRemoved2 = tlu.editCompID(strTempCompIDT, vRemoved);
				strTempCompIDT = tlu.adjustCompIDVal(tlu.removeCompID(strTempCompIDT, vRemoved2), vRemoved);
				
				
				// TARGET MATCH
				vResult = constructTemplateStringAndChunks((Vector)vMatchTokenT.clone(),
					(Vector)vDiffMatchT2.clone(), false, null, false);
				StringBuffer sbTempT2 = (StringBuffer)vResult.get(0);
				Vector vTargetChunks2 = (Vector)vResult.get(1);

				
			
			
			
	//REPLACE WITH THIS

			TExtChunkSplitter txtChunkSplit1, txtChunkSplit2;
			txtChunkSplit1 = new TExtChunkSplitter(bSourceIsFilipino, tlu);
			txtChunkSplit2 = new TExtChunkSplitter(bSourceIsFilipino, tlu);
			Vector vChosen1 = null, vChosen2 = null;
			Vector vTempS1 = null, vTempS2 = null;
			Vector vTempT1 = null, vTempT2 = null;
			if(txtChunkSplit1.alignChunks(vSourceChunks1, vTargetChunks1, hAlignment, 1) &&
				txtChunkSplit2.alignChunks(vSourceChunks2, vTargetChunks2, hMatchAlignment, 2)){
				
				//System.out.println("BOTH WITH FINAL RESULTS");
				Vector vPossRes1 = txtChunkSplit1.getFinalResult(),
					vPossRes2 = txtChunkSplit2.getFinalResult();
				//System.out.println(vPossRes1);
				//System.out.println(vPossRes2);
				Vector vChosenRes = tlu.chooseAmongAlSplit(vPossRes1, vPossRes2);
				
				if(vChosenRes != null){
					//System.out.println("WITH CHOSEN RESULT");
					vChosen1 = (Vector)vChosenRes.get(0);
					vChosen2 = (Vector)vChosenRes.get(1);
					
					vSourceChunks1 = (Vector)vChosen1.get(1);
					vTargetChunks1 = (Vector)vChosen1.get(2);
					vTempS1 = (Vector)vChosen1.get(3);
					vTempT1 = (Vector)vChosen1.get(4);
					Vector vDummy1 = tlu.adjustChunkDiv(sbTempS1.toString(), 
						(Vector)vSourceChunks1.clone(), strTempCompIDS);
					Vector vDummy2 = tlu.adjustChunkDiv(sbTempT1.toString(), 
						(Vector)vTargetChunks1.clone(), strTempCompIDT);
					
					if(vDummy1 == null || vDummy2 == null)
						return bReturnSuccess;

					//System.out.println("vDummy1 -->" + vDummy1);
					
					if(vDummy1.size() == 2){
						sbTempS1 = (StringBuffer)vDummy1.remove(1);
						strTempCompIDS = new String((String)vDummy1.remove(0));
					}
					else
						sbTempS1 = (StringBuffer)vDummy1.remove(0);
						
					if(vDummy2.size() == 2){
						sbTempT1 = (StringBuffer)vDummy2.remove(1);
						strTempCompIDT = new String((String)vDummy2.remove(0));
					}
					else
						sbTempT1 = (StringBuffer)vDummy2.remove(0);
					
					vSourceChunks2 = (Vector)vChosen2.get(1);
					vTargetChunks2 = (Vector)vChosen2.get(2);
					vTempS2 = (Vector)vChosen2.get(3);
					vTempT2 = (Vector)vChosen2.get(4);
					vDummy1 = tlu.adjustChunkDiv(sbTempS2.toString(), 
						(Vector)vSourceChunks2.clone(), "none");
					vDummy2 = tlu.adjustChunkDiv(sbTempT2.toString(), 
						(Vector)vTargetChunks2.clone(), "none");

					if(vDummy1 == null || vDummy2 == null)
						return bReturnSuccess;

					sbTempS2 = (StringBuffer)vDummy1.remove(0);
					sbTempT2 = (StringBuffer)vDummy2.remove(0);

					vDummy1 = null;
					vDummy2 = null;
				}
				else
					return bReturnSuccess;
			}
			else
				return bReturnSuccess;
	//END REPLACE WITH THIS

			//System.out.println("vSourceChunks1 -->" + vSourceChunks1);
			//System.out.println("vSourceChunks2 -->" + vSourceChunks2);
			//System.out.println("vTargetChunks1 -->" + vTargetChunks1);
			//System.out.println("vTargetChunks2 -->" + vTargetChunks2);
		//	javax.swing.JOptionPane.showMessageDialog(null, "stop");
			
			//REPLACE WITH THIS2
			Vector vAlChunkS1 = new Vector(1), vAlChunkT1 = new Vector(1);
			Vector vAlChunkS2 = new Vector(1), vAlChunkT2 = new Vector(1);

			//setdomain
			for(Iterator it = vSourceChunks1.iterator(); it.hasNext(); ){
				TExtChunk outer = (TExtChunk)it.next();
				for(int kCtr = 0; kCtr < vTempS1.size(); kCtr++){
					TExtChunk inner = (TExtChunk)vTempS1.get(kCtr);
					if(outer.equals(inner)){
						vAlChunkS1.add(inner);
						TExtChunk cAlTarget = (TExtChunk)vTempT1.get(kCtr);
						cAlTarget.setDomain(inner.domain());
						vAlChunkT1.add(cAlTarget);
						vTempS1.remove(kCtr);
						vTempT1.remove(kCtr);
						break;
					}
				}
			}

			// setdomain
			for(Iterator it = vSourceChunks2.iterator(); it.hasNext(); ){
				TExtChunk outer = (TExtChunk)it.next();
				for(int kCtr = 0; kCtr < vTempS2.size(); kCtr++){
					TExtChunk inner = (TExtChunk)vTempS2.get(kCtr);
					if(outer.equals(inner)){
						vAlChunkS2.add(inner);
						TExtChunk cAlTarget = (TExtChunk)vTempT2.get(kCtr);
						cAlTarget.setDomain(inner.domain());
						vAlChunkT2.add(cAlTarget);
						vTempS2.remove(kCtr);
						vTempT2.remove(kCtr);
						break;
					}
				}
			}
			
			
	//END REPLACE WITH THIS2
			
			
			int nSize = vAlChunkS1.size();
			TExtChunk tempTemp;
			TExtChunk tempInput;
			for(int kCtr = 0; kCtr < nSize; kCtr++){
				tempTemp = (TExtChunk)vAlChunkS1.get(kCtr);
				tempInput = (TExtChunk)vAlChunkS2.get(kCtr);
				tempInput.setDomain(tempTemp.domain());
				vAlChunkS2.setElementAt(tempInput, kCtr);

				tempTemp = (TExtChunk)vAlChunkT1.get(kCtr);
				tempInput = (TExtChunk)vAlChunkT2.get(kCtr);
				tempInput.setDomain(tempTemp.domain());
				vAlChunkT2.setElementAt(tempInput, kCtr);
			}

	//REPLACE WITH THIS
			Hashtable htChunkAlignment1 = new Hashtable();
			Hashtable htChunkAlignment2 = new Hashtable();
			htChunkAlignment1.put((Vector)vAlChunkS1.clone(), (Vector)vAlChunkT1.clone());
			htChunkAlignment2.put((Vector)vAlChunkS2.clone(), (Vector)vAlChunkT2.clone());
	//END REPLACE WITH THIS


			//to construct the template with the pattern: [I] ate [meat] :to: [1] ate [2]		
			StringBuffer sbTemplateS = new StringBuffer(),
				sbTemplateT = new StringBuffer();
			StringBuffer sbChunk = new StringBuffer();
			String strTempS = sbTempS1.toString(), strTempT = sbTempT1.toString();
			boolean bChunkScan = false;
			for(int p=0; p<strTempS.length(); p++) {
				if(!bChunkScan)
					sbTemplateS.append(strTempS.charAt(p));
				else
					sbChunk.append(strTempS.charAt(p));
				if(strTempS.charAt(p)=='[') {
					sbChunk = new StringBuffer();
					bChunkScan = true;
				} else if(strTempS.charAt(p)==']') {
					sbChunk.deleteCharAt(sbChunk.length()-1);
					for(int q=0; q<vAlChunkS1.size(); q++) {
						TExtChunk cCheck = (TExtChunk)vAlChunkS1.get(q);
						if(cCheck.toString().equals(sbChunk.toString())) {
							sbTemplateS.append(cCheck.domain().substring(1, cCheck.domain().length()-1));
							break;
						}
					}
					sbTemplateS.append(strTempS.charAt(p));
					bChunkScan = false;
				}
			}

			
			sbChunk = new StringBuffer();
			bChunkScan = false;
			for(int r=0; r<strTempT.length(); r++) {
				if(!bChunkScan)
					sbTemplateT.append(strTempT.charAt(r));
				else
					sbChunk.append(strTempT.charAt(r));
				if(strTempT.charAt(r)=='[') {
					sbChunk = new StringBuffer();
					bChunkScan = true;
				} else if(strTempT.charAt(r)==']') {
					sbChunk.deleteCharAt(sbChunk.length()-1);
					for(int s=0; s<vAlChunkT1.size(); s++) {
						TExtChunk cCheck = (TExtChunk)vAlChunkT1.get(s);
						if(cCheck.toString().equals(sbChunk.toString())) {
							sbTemplateT.append(cCheck.domain().substring(1, cCheck.domain().length()-1));
							break;
						}
					}
					sbTemplateT.append(strTempT.charAt(r));
					bChunkScan = false;
				}
			}


			StringBuffer sbTemplateS2 = new StringBuffer(),
				sbTemplateT2 = new StringBuffer();
			StringBuffer sbChunk2 = new StringBuffer();
			String strTempS2 = sbTempS2.toString(), strTempT2 = sbTempT2.toString();
			bChunkScan = false;
			for(int p=0; p<strTempS2.length(); p++) {
				if(!bChunkScan)
					sbTemplateS2.append(strTempS2.charAt(p));
				else
					sbChunk2.append(strTempS2.charAt(p));
				if(strTempS2.charAt(p)=='[') {
					sbChunk2 = new StringBuffer();
					bChunkScan = true;
				} else if(strTempS2.charAt(p)==']') {
					sbChunk2.deleteCharAt(sbChunk2.length()-1);
					for(int q=0; q<vAlChunkS1.size(); q++) {
						TExtChunk cCheck = (TExtChunk)vAlChunkS2.get(q);
						if(cCheck.toString().equals(sbChunk2.toString())) {
							sbTemplateS2.append(cCheck.domain().substring(1, cCheck.domain().length()-1));
							break;
						}
					}
					sbTemplateS2.append(strTempS2.charAt(p));
					bChunkScan = false;
				}
			}

			
			sbChunk2 = new StringBuffer();
			bChunkScan = false;
			for(int r=0; r<strTempT2.length(); r++) {
				if(!bChunkScan)
					sbTemplateT2.append(strTempT2.charAt(r));
				else
					sbChunk2.append(strTempT2.charAt(r));
				if(strTempT2.charAt(r)=='[') {
					sbChunk2 = new StringBuffer();
					bChunkScan = true;
				} else if(strTempT2.charAt(r)==']') {
					sbChunk2.deleteCharAt(sbChunk2.length()-1);
					for(int s=0; s<vAlChunkT2.size(); s++) {
						TExtChunk cCheck = (TExtChunk)vAlChunkT2.get(s);
						if(cCheck.toString().equals(sbChunk2.toString())) {
							sbTemplateT2.append(cCheck.domain().substring(1, cCheck.domain().length()-1));
							break;
						}
					}
					sbTemplateT2.append(strTempT2.charAt(r));
					bChunkScan = false;
				}
			}
				
				TExtTemplate tSource = new TExtTemplate(sbTemplateS.toString(), strTempCompIDS),
					tTarget = new TExtTemplate(sbTemplateT.toString(), strTempCompIDT);
				
				TExtTemplate tSource2 = new TExtTemplate(sbTemplateS2.toString(), strTempCompIDS),
				tTarget2 = new TExtTemplate(sbTemplateT2.toString(), strTempCompIDT); 
			
			/* Update the database with the newly formulated template and chunks.
			 */
				
			boolean bCheck = true;
			//v: added strDomain
			if (bDTTL){
				if(bSourceIsFilipino){
					bCheck = updateDatabase(tTarget, tSource, htChunkAlignment1, htChunkAlignment2, false, strDomain);
					updateDatabase(tTarget2, tSource2, htChunkAlignment1, htChunkAlignment2, false, strDomain);
				}				  	
				else {
					bCheck = updateDatabase(tSource, tTarget, htChunkAlignment1, htChunkAlignment2, true, strDomain);
					updateDatabase(tSource2, tTarget2, htChunkAlignment1, htChunkAlignment2, true, strDomain);
					
				}			 	
			}else{
				if(bSourceIsFilipino)				
				  	bCheck = updateDatabase(tTarget, tSource, htChunkAlignment1, htChunkAlignment2, false, strDomain);
				else 
				 	bCheck = updateDatabase(tSource, tTarget, htChunkAlignment1, htChunkAlignment2, true, strDomain);
				
				//TODO: hash marker lang
				Vector vs = new Vector(1);
				Vector vt = new Vector(1);
				tSource.adjustTemplateCompound();
				tTarget.adjustTemplateCompound();
				vs.add(tSource);
				vt.add(tTarget);
				hSTTLAlignmentS = getAlignmentInTemplates(vs, vt, bSourceIsFilipino);
				hSTTLAlignmentT = getAlignmentInTemplates(vt, vs, !bSourceIsFilipino);
				
				
				for(Iterator it = tSource.iterator(); it.hasNext();){
					Object o = it.next();
					if (o instanceof String) vExcludeS.add(o);		
				}
				
				for(Iterator it = tTarget.iterator(); it.hasNext();){
					Object o = it.next();
					if (o instanceof String) vExcludeT.add(o);		
				}
				
				
				hExcludeS1 = getExcludeList(strTempS, vTempTokenS);
				hExcludeS2 = getExcludeList(strTempS2, vMatchTokenS);
				hExcludeT1 = getExcludeList(strTempT, vTempTokenT);
				hExcludeT2 = getExcludeList(strTempT2, vMatchTokenT);
		
				bReturnSuccess = bCheck;
				
			}
			
			
			if(bCheck) {
				Vector vTemp = new Vector(1);
				vTemp.addElement(alChunk);
				vTemp.addElement(alMatch);
				vTemp.addElement(new AlignedTemplate(tSource,tTarget));
				
				Vector vChunks = new Vector(1);
				while(!vAlChunkS1.isEmpty() && !vAlChunkT1.isEmpty())
					vChunks.addElement(new AlignedChunk((TExtChunk)vAlChunkS1.remove(0),
						(TExtChunk)vAlChunkT1.remove(0)));
				while(!vAlChunkS2.isEmpty() && !vAlChunkT2.isEmpty())
					vChunks.addElement(new AlignedChunk((TExtChunk)vAlChunkS2.remove(0),
						(TExtChunk)vAlChunkT2.remove(0)));
				vTemp.addElement(vChunks);
				
				String strTarget;
				String strSource;
				if(bSourceIsFilipino){
					strSource = "Filipino";
					strTarget = "English";
				}else{
					strSource = "English";
					strTarget = "Filipino";
				}
				
				/*
				 * v add: delete chunk match.
				 * this is needed because once it matches, a new chunk based on the match
				 * will be created. the new chunk is more generalized. the old chunk is
				 * no longer needed
				 */
				
				
				DBConnector.update("DELETE FROM Chunks WHERE "
						+ "chunk" + strSource + " = '" 
						+ alMatch.getSource().toString().replace("'","''") + "'"
						+ " AND chunk" + strTarget + " = '" 
						+ alMatch.getTarget().toString().replace("'","''") + "'"
						+ " AND compID" + strSource + " = '" 
						+ alMatch.getSource().getCompoundIdentifier() + "'"
						+ " AND compID" + strTarget + " = '" 
						+ alMatch.getTarget().getCompoundIdentifier() + "'");
			}
			

			
			if(nDTTL==1)return bReturnSuccess;
			else{
				bDTTL = true;
			}
			
		}
			
		return false;
	}
	
	
	
	
	

	protected Hashtable getAlignmentInTemplates(Vector vSourceChunks1, Vector vTargetChunks1, boolean sourceIsFil){
		Vector vSourceTokens = new Vector();
		Vector vTargetTokens = new Vector();
		String strDummy = null;
		Hashtable hTemplate;
		Vector vUpdatedSChunks = new Vector(vSourceChunks1.size());
		Vector vUpdatedTChunks = new Vector(vTargetChunks1.size());
		
		TExtAligner aligner;
		
		if(sourceIsFil)
			aligner = new TExtAligner('e');
		else
			aligner = new TExtAligner('f');
			
		for(Iterator it1 = vSourceChunks1.iterator(); it1.hasNext(); ){
			Vector cSource = (Vector)it1.next();
			if(cSource.size() == 0);
				
			else
				for(Iterator it2 = cSource.iterator(); it2.hasNext(); )
					vSourceTokens.add(it2.next().toString());
			vSourceTokens.add(strDummy);
		}
		
		for(Iterator it1 = vTargetChunks1.iterator(); it1.hasNext(); ){
			Vector cTarget = (Vector)it1.next();
			if(cTarget.size() == 0);
			else
				for(Iterator it2 = cTarget.iterator(); it2.hasNext(); )
					vTargetTokens.add(it2.next().toString());
			vTargetTokens.add(strDummy);
		}

		return aligner.align(vSourceTokens, vTargetTokens, false);
	}
	
	
	
	
	
	
	
	
	
	
	private Vector constructPattern(Vector vTokens, Vector vSimilarities) {
		Vector vReturn = new Vector(1);
		
		boolean lastWasChunk = false;
		int nMatchPtr = 0, nTempChunkCnt = 0;
		Vector vPattern = new Vector(1);
		for(int f=0; f<vTokens.size(); f++)
			if(nMatchPtr<vSimilarities.size() 
				&& vSimilarities.get(nMatchPtr).toString().equalsIgnoreCase(vTokens.get(f).toString())) {
				//System.out.print("String");
				vPattern.add(new String());
				nMatchPtr++;
				lastWasChunk = false;
			} else {
				if(!lastWasChunk) {
					//System.out.print("Chunk");
					vPattern.add(new TExtChunk());
					nTempChunkCnt++;
				}
				lastWasChunk = true;
			}
		//System.out.println();
		
		vReturn.addElement(vPattern);
		vReturn.addElement(new Integer(nTempChunkCnt));
		
		return vReturn;
	}
	
	
	
	/**
	 * gets the strings which should not be included.
	 * input: the [Republic of the Philippines is a] country [in Asia]
	 * return: [the=0 , country=4]
	 * 
	 * @param strPattern something like: the [Republic of the Philippines is a] country [in Asia]
	 * @return a hashtable containing the string (key) index (value)
	 */
	public Hashtable getExcludeList(String strPattern, Vector vTokens){
		Hashtable h = new Hashtable(1);
		String strToken[] = strPattern.split(" ");
		
		boolean bracket = false;
		boolean bAdd = false;;
		int j=0; //iterator for string, i need to skip if token is compound
		for(int i=0; i<vTokens.size(); i++){
			String str = strToken[j];
			bAdd=true;
			if (!bracket && str.charAt(0) == '[') {
				bracket = true;
				bAdd = false;
			}
			
			if (bracket) {
				bAdd=false;
				if (str.charAt(str.length()-1) == ']'){
					bracket = false;
					bAdd=false;
				}
			}
			
			if(bAdd)h.put(vTokens.get(i).toString(), new Integer(i));		
			j+=vTokens.get(i).toString().split(" ").length;
			
		}
		return h;
	}
	
	
	
	
	
	/**
	 * Obtains the String matches between two sentences with the token Vector
	 *	parameters.
	 *
	 * @see deriveTemplate(AlignedSentence a, AlignedSentence a)
	 */
	private Vector getStringMatches(Vector vCurrent, Vector vMatch) {
		Vector vMatches = new Vector(1);
		
		boolean hasMatch = false;
		int d = 0;
		for(int a=0; a<vCurrent.size(); a++) {
			String strComp = (String)vCurrent.get(a);
			hasMatch = false;
			for(int b=d; !hasMatch && b<vMatch.size(); b++) {
				String strMatch = (String)vMatch.get(b);
				if(strComp.equalsIgnoreCase(strMatch)) {
					hasMatch = true;
					d = b;
					do {
						a++; d++;
						vMatches.add(strComp);
				
						if(a>=vCurrent.size() || d>=vMatch.size())
							break;
						strComp = (String)vCurrent.get(a);
						strMatch = (String)vMatch.get(d);
					} while(strComp.equalsIgnoreCase(strMatch));
					d++;
				}
			}
		}
		
		return vMatches;
	}	
	
	/**
	 * 
	 * @param tEnglish
	 * @param tFilipino
	 * @param htNewChunks1
	 * @param htNewChunks2
	 * @param bSourceIsEnglish
	 * @param strDomainOfMatch the domain of the existing chunk will be 
	 * 		used as the domain for the new chunk
	 * @return
	 */
	private boolean updateDatabase(TExtTemplate tEnglish, TExtTemplate tFilipino,
			Hashtable htNewChunks1, Hashtable htNewChunks2, 
			boolean bSourceIsEnglish, String strDomainOfMatch) {
			
			String strTempE = tEnglish.toString();
			String strTempF = tFilipino.toString();
		
			Vector vDoNotDelete = new Vector(1);
			boolean bDeleteWholeParent=true;
			
			boolean bRefinementDone = false;
			Hashtable hOldNewDomainMap = new Hashtable();
			boolean isSuccessful = true;
			Vector vBlankFillDomains = new Vector(1);
			Vector vWholeDomainsUsed = new Vector(1);
			DBConnector.setAutoCommit(false);
					
			String strSource = new String(), strTarget = new String();
			if(bSourceIsEnglish) {
				strSource = "English";
				strTarget = "Filipino";
			} else {
				strSource = "Filipino";
				strTarget = "English";			
			}
			try {
				Vector vAlternatives = new Vector(1);
				if(htNewChunks1 != null && htNewChunks2 != null) {
					
					// RESOLVE START
					Vector vKeys = new Vector(1), vElements = new Vector(1), vTemp;
					vKeys.addAll((Vector)htNewChunks1.keys().nextElement());
					vElements.addAll((Vector)htNewChunks1.elements().nextElement());
					vKeys.addAll((Vector)htNewChunks2.keys().nextElement());
					vElements.addAll((Vector)htNewChunks2.elements().nextElement());

			
					String strInsertOrder = new String();
					if(bSourceIsEnglish)
						strInsertOrder = "chunkEnglish, chunkFilipino";
					else
						strInsertOrder = "chunkFilipino, chunkEnglish";
					
					for(int b=0; isSuccessful && b<vKeys.size(); b++) {
						TExtChunk cSourceChunk = (TExtChunk)vKeys.get(b);
						String strSDomTemp = cSourceChunk.domain();
						if(strSDomTemp.startsWith("[") && strSDomTemp.endsWith("]"))
							cSourceChunk.setDomain(strSDomTemp.substring(1, strSDomTemp.length()-1));
						
						TExtChunk cCurrTarget = (TExtChunk)vElements.get(b);
						String strTDomTemp = cCurrTarget.domain();
						if(strTDomTemp.startsWith("[") && strTDomTemp.endsWith("]"))
							cCurrTarget.setDomain(strTDomTemp.substring(1, strTDomTemp.length()-1));
						String strDomain = cCurrTarget.domain();
						String strDomExtS = new String(""), strDomExtT = new String("");
						if(!cSourceChunk.domain().contains("."))
							strDomExtS = new String(".");
						if(!strDomain.contains("."))
							strDomExtT = new String(".");

						
						String strCWEng;
						String strCWFil;
						if(bSourceIsEnglish){
							strCWFil = cCurrTarget.getCompoundIdentifier();
							strCWEng = cSourceChunk.getCompoundIdentifier();
						}
						else{
							strCWEng = cCurrTarget.getCompoundIdentifier();
							strCWFil = cSourceChunk.getCompoundIdentifier();
						}
//					
						if(strCWEng==null)
							strCWEng = new String("none");
						if(strCWFil==null)
							strCWFil = new String("none");
						boolean bToUpdateBlank = false;
						ResultSet rsTemp = DBConnector.select("Select * From Chunks"
							+ " Where chunk" + strSource + "='"
							+ cSourceChunk.toString().replace("'","''") 
							+ "' And chunk" + strTarget + "='"
							+ cCurrTarget.toString().replace("'","''")
							+ "' And compIDEnglish='" + strCWEng 
							+ "' And compIDFilipino='" + strCWFil + "'");

						if(strDomain.contains(".")) {
							int nIndex = strDomain.indexOf('.');
							strDomain = strDomain.substring(0,nIndex);
						}
						ResultSet rsDom = DBConnector.select("Select * From Chunks "
							+ "Where domain Like '" + strDomain + ".%'");
						if(rsDom.next()) {
							double dMax = 0.0;
							do {
								double dTemp = rsDom.getDouble("domain");
								if(dTemp>dMax)
									dMax = dTemp;
							} while(rsDom.next());
								StringTokenizer stTemp = new 
									StringTokenizer(""+dMax,".");
								String strGarbage = stTemp.nextToken();
								strDomain = strDomain+"."
									+(Integer.parseInt(stTemp.nextToken())+1);
							
						} else {
							strDomain = strDomain+".1";
							if(vBlankFillDomains.contains(strDomain))
								bToUpdateBlank = true;
						}
						
					//System.out.println("domain at this point is "+strDomain);
						if(!rsTemp.next()) {
							String strInsertOrder2;
							
							if(bSourceIsEnglish)
								strInsertOrder2 = " constCntEng, constCntFil";
							else
								strInsertOrder2 = " constCntFil, constCntEng";
								
							int nCnstCntS = 0, nCnstCntT = 0;
							
							for(Iterator it = cSourceChunk.iterator(); it.hasNext(); )
								if(it.next() instanceof String)
									nCnstCntS++;
							for(Iterator it = cCurrTarget.iterator(); it.hasNext(); )
								if(it.next() instanceof String)
									nCnstCntT++;
							
							if(bToUpdateBlank) 
								vBlankFillDomains.remove(strDomain);

							
							//V call recurssion start
							TExtChunk cS = cSourceChunk;
							cS.setDomain(strDomain);
							TExtChunk cT = cCurrTarget;
							cT.setDomain(strDomain);

							bRefinementDone = chunkRefinement(new AlignedChunk(cS, cT), null, null, hOldNewDomainMap);			
							//V call recurssion end
							
							if (!bRefinementDone) {
								isSuccessful = DBConnector.update("Insert Into Chunks"
										+ " (" + strInsertOrder + ", domain,"
										+ " compIDEnglish, compIDFilipino," + strInsertOrder2
										+ ") Values ('" + cSourceChunk.toString().replace("'","''")
										+ "','"	+ cCurrTarget.toString().replace("'","''")
										+ "','" + strDomain + "','" + strCWEng
										+ "','" + strCWFil + "'," + nCnstCntS + "," + nCnstCntT + ")");
								//add the domains
								strTempE = strTempE.replace("["+stripSquareBrackets(strSDomTemp)+"]",  "["+stripSquareBrackets(strSDomTemp)+"/"+strDomain+"]");
								strTempF = strTempF.replace("["+stripSquareBrackets(strSDomTemp)+"]",  "["+stripSquareBrackets(strSDomTemp)+"/"+strDomain+"]");
								vWholeDomainsUsed.add(stripSquareBrackets(strSDomTemp));
								
								// 94.1 changed to 32.2 ; 94 will be changed to 32.2
								// 94.2 will be written. but it will not be referenced because 94
								// was replaced by 32.2
								// maybe replacing should be done after all chunks have been refined and written
							}else {
								/* if refinement was done, the domain of the product chunk changed
								 * because it's original form was deleted because of generalization.
								 * MOD PARENT HERE. 
								 * 
								 */
								
								println("5700 CODE2: modify parent from CR-DTTL");
								/*TODO: 94.1 should be 93.3, but in template is 94.
								 * if the chunkDB contains many 94s eg 94.1 94.2, retain 94, add the new domain
								 * if the chunkDB has no 94, replace it with the new domain.
								 */
								//replace ASAP if it is not whole
								if(TExtUtility.compareBaseDomain(stripSquareBrackets(strSDomTemp), (String)hOldNewDomainMap.get(strDomain))){
									vWholeDomainsUsed.add(stripSquareBrackets(strSDomTemp));
									strTempE = strTempE.replace("["+stripSquareBrackets(strSDomTemp)+"]",  "["+stripSquareBrackets(strSDomTemp)+"/"+strDomain+"]");
									strTempF = strTempF.replace("["+stripSquareBrackets(strSDomTemp)+"]",  "["+stripSquareBrackets(strSDomTemp)+"/"+strDomain+"]");
									
								}
								
								
								if(strSDomTemp.contains(".")){
									//strDomain replace? or strSDomTemp?
									strTempE = strTempE.replace("["+strDomain+"]","["+(String)hOldNewDomainMap.get(strDomain) +"]");
									strTempF = strTempF.replace("["+strDomain+"]","["+(String)hOldNewDomainMap.get(strDomain) +"]");
									//the replacement was not added
									
									String strWholeVar = strDomain.substring(0, strDomain.indexOf('.'));
									strTempE = strTempE.replace("["+strWholeVar+"]","["+strWholeVar+"/"+(String)hOldNewDomainMap.get(strDomain) +"]");
									strTempF = strTempF.replace("["+strWholeVar+"]","["+strWholeVar+"/"+(String)hOldNewDomainMap.get(strDomain) +"]");

									
								}else{
									//i need to add it,, does this even happen? yes
									String strWholeVar = strDomain.substring(0, strDomain.indexOf('.'));
									strTempE = strTempE.replace("["+strWholeVar+"]","["+strWholeVar+"/"+(String)hOldNewDomainMap.get(strDomain) +"]");
									strTempF = strTempF.replace("["+strWholeVar+"]","["+strWholeVar+"/"+(String)hOldNewDomainMap.get(strDomain) +"]");
								}
							}
						} else {
							String strDupDomain = rsTemp.getString("domain");
							
							/* 
							 * This part is highly modified... danger? yes 
							 */
							
							//should only be used by DTTL
							boolean bDupIsSimilar = true;
							if (strDupDomain.contains("."))
							 bDupIsSimilar = strDupDomain.substring(0, strDupDomain.indexOf(".")).equals(cSourceChunk.domain());
					
							if(!bDupIsSimilar)	{
								vAlternatives.addElement(cSourceChunk.domain());
								vAlternatives.addElement(strDupDomain);
								//when do i call these?
							}else{
								//if the duplicate is a child of the whole domain, add it.
								String strWholeVar = strDomain.substring(0, strDomain.indexOf('.'));
								strTempE = strTempE.replace("["+strWholeVar+"]","["+strWholeVar+"/"+strDupDomain+"]");
								strTempF = strTempF.replace("["+strWholeVar+"]","["+strWholeVar+"/"+strDupDomain+"]");

								
							}
						}
					}
				
					
					while(!vBlankFillDomains.isEmpty()) {
						String strDomFill = vBlankFillDomains.remove(0).toString();
						/*
						isSuccessful = DBConnector.update("Insert Into Chunks"
							+ " (" + strInsertOrder + ", domain) Values ('','','" 
							+ strDomFill + "')");
						*/
					}
				} // end insert chunks
				
				if((tEnglish != null) && (tFilipino != null) && isSuccessful) {
					
					String strTemplE = strTempE, strTemplF = strTempF;

					for(int a=0; a<vAlternatives.size(); a+=2) {
						strTemplE = strTemplE.replace("["+vAlternatives.get(a)+"/",
							"["+vAlternatives.get(a)+"/"+vAlternatives.get(a+1)+"/");
						strTemplF = strTemplF.replace("["+vAlternatives.get(a)+"/",
							"["+vAlternatives.get(a)+"/"+vAlternatives.get(a+1)+"/");
						strTemplE = strTemplE.replace("["+vAlternatives.get(a)+"]",
							"["+vAlternatives.get(a)+"/"+vAlternatives.get(a+1)+"]");
						strTemplF = strTemplF.replace("["+vAlternatives.get(a)+"]",
							"["+vAlternatives.get(a)+"/"+vAlternatives.get(a+1)+"]");
					}
					
					//generalize domains here
					// remove the whole domains which are not in this list domain(vector domains used)
					
	//				strTemplE = removeWholeDomainsNotInThisList(strTemplE, vWholeDomainsUsed);
	//				strTemplF = removeWholeDomainsNotInThisList(strTemplF, vWholeDomainsUsed);
					
					strTemplE = generalizeDomainsDTS(strTemplE);
					strTemplF = generalizeDomainsDTS(strTemplF);
					
					strTemplE = tlu.generalizeDomains(strTemplE);
					strTemplF = tlu.generalizeDomains(strTemplF);
					
					String strCWEng = tEnglish.getCompoundIdentifier(),
						   strCWFil = tFilipino.getCompoundIdentifier();
					
					ResultSet rsTemp = DBConnector.select("Select * From Chunks Where"
						+ " chunkEnglish='" + strTemplE.replace("'","''")
						+ "' And chunkFilipino='" 
						+ strTemplF.replace("'","''") 
						+ "' And compIDEnglish='" + strCWEng + "' And compIDFilipino='" 
						+ strCWFil + "'");
						
					if(!rsTemp.next()) {					
						int nCnstCntE = 0, nCnstCntF = 0;
						
						for(Iterator it = tEnglish.iterator(); it.hasNext(); )
							if(it.next() instanceof String)
								nCnstCntE++;
						for(Iterator it = tFilipino.iterator(); it.hasNext(); )
							if(it.next() instanceof String)
								nCnstCntF++;
						
						
						//if refinement done, cook [45] with
						
						//insert domain here. template kasi to dati
						isSuccessful = DBConnector.update("Insert Into Chunks"
							+ " (chunkEnglish, chunkFilipino, compIDEnglish,"
							+ " compIDFilipino, constCntEng, constCntFil, domain) Values ('" 
							+ strTemplE.replace("'","''") + "','" 
							+ strTemplF.replace("'","''") + "','" 
							+ strCWEng + "','" + strCWFil + "'," + nCnstCntE
							+ "," + nCnstCntF +", '"+strDomainOfMatch +"')");
						
						
					}
				}
				
				if(!isSuccessful)
					DBConnector.rollback();
				else
					DBConnector.commit();
			} catch(SQLException sqle) {
				System.out.println("Generic update error: "+sqle.getMessage());
				DBConnector.rollback();
			}
			
			DBConnector.setAutoCommit(true);
			
			return isSuccessful;
		}
	
	
	public String removeWholeDomainsNotInThisList(String strCTValue, Vector vInclude){


		String strCTValue2 = strCTValue;
		String str = strCTValue;
		boolean bProceed = str.contains("[");

		while( bProceed) {
			int nBegin = str.indexOf('[');
			int nEnd = str.indexOf(']');
			String strDomain = str.substring(nBegin+1, nEnd); // 99/22.1/99.1
			String strNewDomain = new String();
			String arrDomains[] = strDomain.split("/");
			String strCurrentWhole = new String();
			boolean bAddDecimals = true;
			for(int i=0; i < arrDomains.length; i++) {

				if (!arrDomains[i].contains(".")) {
					strCurrentWhole = arrDomains[i];

					if (vInclude.contains(strCurrentWhole)){
						if(strNewDomain.equals("")){
							strNewDomain+=strCurrentWhole;
						}else{
							strNewDomain+="/"+strCurrentWhole;
						}						
					}
				}else{
						if(strNewDomain.equals("")){
							strNewDomain+=arrDomains[i];
						}else{
							strNewDomain+="/"+arrDomains[i];
						}			
				}//endif
			}//end for
			
			strCTValue2 = strCTValue2.replace(strDomain, strNewDomain);
				
			str = str.substring(nEnd+1, str.length());
			bProceed = str.contains("[");
		
		}//end while
		
		return strCTValue2;
		
	}//end func
	
	
	
	
	
	
	
	private Vector checkPattern(Vector vTokens, Vector vSimilarities,
			Vector vTemComparison) {
			Vector vReturn = new Vector(1);
			
			int h = 0;
			boolean lastWasChunk = false, bCorrect = true;
			int nMatchPtr = 0, nMatchSChunkCnt = 0;
			Vector vPattern = new Vector(1);
			for(int g=0; g<vTokens.size() && bCorrect; g++)
				if(nMatchPtr<vSimilarities.size() 
					&& vSimilarities.get(nMatchPtr).toString().equalsIgnoreCase(vTokens.get(g).toString())) {
					//System.out.print("String");
					if(lastWasChunk)
						h++;
					if(h == vTemComparison.size()){
						bCorrect = false;
						break;
					}
					if(vTemComparison.get(h) instanceof TExtChunk)
						bCorrect = false;
					nMatchPtr++;
					lastWasChunk = false;
					h++;
				} else {
					if(!lastWasChunk){
						//System.out.print("Chunk");
						nMatchSChunkCnt++;
					}
					if(h == vTemComparison.size()){
						bCorrect = false;
						break;
					}
					if(vTemComparison.get(h) instanceof String)
						bCorrect = false;
					lastWasChunk = true;
				}
			
			vReturn.addElement(new Boolean(bCorrect));
			vReturn.addElement(new Integer(nMatchSChunkCnt));
			
			return vReturn;
		}
	
	
	
	
	
	/**
	 * Constructs the template string (StringBuffer version) and builds the 
	 *	corresponding TExtChunk objects.
	 *
	 * @see deriveTemplate(AlignedSentence, AlignedSentence)
	 */
	private Vector constructTemplateStringAndChunks(Vector vTokens,
		Vector vSimilarities, boolean bWithBounds, String strCompID,
		boolean bIsFirstProcedure) {
		Vector vReturn = new Vector(1);
		
		Vector vChunks = new Vector(1), vTempChunk = new Vector(1),
			vRemoved = new Vector(1);
		StringBuffer sbTemp = new StringBuffer();
		String strNextChunk = new String();
		int nChunkTracker, nMatchPtr = 0;
		int nLBound = 0, nUBound, nStart = -1;
		if(bIsFirstProcedure)
			nChunkTracker = 0;
		else
			nChunkTracker = Integer.parseInt(tlu.returnNextChunk());
		boolean lastWasChunk = false;
		
		for(int l=0; l<vTokens.size(); l++){
			if(nMatchPtr<vSimilarities.size() 
				&& vSimilarities.get(nMatchPtr).toString().equalsIgnoreCase(vTokens.get(l).toString())) { 
				if(lastWasChunk) {
					if(bWithBounds)
						vRemoved.add(nStart + "-" + (nLBound-1));
					sbTemp.deleteCharAt(sbTemp.length()-1);
					sbTemp.append("]");
					sbTemp.append(" ");
					TExtChunk cTemp = new TExtChunk();
	//System.out.println(">>>   "+strNextChunk+"   <<<");
					cTemp.setChunk(vTempChunk, "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
					vChunks.addElement(cTemp);
				}
				sbTemp.append(vTokens.get(l));
				sbTemp.append(" ");
				nMatchPtr++;
				lastWasChunk = false;
				if(bWithBounds)
					if(vTokens.get(l).toString().contains(" "))
						nLBound += (new StringTokenizer(vTokens.get(l).toString())).countTokens()-1;
			} else {
				if(!lastWasChunk) {
					if(bWithBounds)
						nStart = nLBound;
					vTempChunk = new Vector(1);
					sbTemp.append("[");
					int nNextChunkDomain = nChunkTracker;
					if(bIsFirstProcedure) 
						nNextChunkDomain += Integer.parseInt(tlu.returnNextChunk());
					strNextChunk = "" + nNextChunkDomain;
					nChunkTracker++;
				}
				sbTemp.append(vTokens.get(l));
				sbTemp.append(" ");
				vTempChunk.addElement(vTokens.get(l));
				if(bWithBounds)
					if(!(strCompID.equals("none")) && vTokens.get(l) instanceof String 
						&& vTokens.get(l).toString().contains(" ")){

						nUBound = nLBound + (new StringTokenizer(vTokens.get(l).toString())).countTokens()-1;
						nLBound = nUBound;
					}
				lastWasChunk = true;
			}
			if(bWithBounds)
				nLBound++;
		}
		if(lastWasChunk) {
			vRemoved.add(nStart + "-" + (nLBound-1));
			sbTemp.deleteCharAt(sbTemp.length()-1);
			sbTemp.append("]");
			TExtChunk cTemp = new TExtChunk();
	//System.out.println(">>>   "+strNextChunk+"   <<<");
			cTemp.setChunk(vTempChunk, "[" + strNextChunk + "]", tlu.makeCompoundIdentifier(vTempChunk));
			vChunks.addElement(cTemp);
		}
		
		vReturn.addElement(sbTemp);
		vReturn.addElement(vChunks);
		vReturn.addElement(vRemoved);
		
		return vReturn;
	}
	
	
	
	/**
	 * does appropriate changes to the domain.
	 * if the domain is [94] but the DB has a lot of 94's
	 * 
	 * @param ctString the template or chunk string that will be modified
	 * @param strDomain the domain which should be replaced or retained
	 * @param hOldNewDomainMap
	 * @return the string that should replace the String of the chunk/template
	 */
	public String replaceDomainForDTS(String ctString, String strDomain, Hashtable hOldNewDomainMap){
		/* per domain in strDomain (94, 94/7, 94/77.1), if the domain is whole, 
		 * check if there is at least one under it (94 -> 94.1, 94.2)
		 * if there is at least 1, just add to the domain
		 * assumption: there can only be one whole number
		 *   if there is CR done, the original has already been deleted.
		 * 	   the other one, brother in STTL, is still there and being used.
		 * 
		 * if it is not whole, change the strDomain to its map
		 * 
		 * note adding should be done last. after all domain iterations
		 */
		
		//TODO: i need the domain of the chunk that was deleted, that should be replaced
		/* i need it because, if there are
		 * 2 non-whole domains, which one should i delete.
		 * 
		 * 
		 */
		boolean bReplaceWhole = false;
		String strDomains[] = strDomain.split("/");
		String strNewDomain = new String();
		Vector vReplaceDomains = new Vector(1);
		
		for(int i=0; i< strDomains.length; i++){
			
			//if whole number, else
			if (!strDomains[i].contains(".")) {
				//search the db for all the uses of the domain
				ResultSet rsDom = DBConnector.select("Select * From Chunks "
					+ "Where domain Like '" + strDomains[i] + ".%'");
				
				try{
					if(rsDom.next()){
						// if there is at least one still using it,
						// do not replace it
					}else{
						vReplaceDomains.add(strDomains[i]);
						bReplaceWhole=true;
					}
					rsDom.getStatement().close();
					rsDom.close();
				}catch(SQLException e){					
				}catch(Exception e){					
				}
			
			}else{
				if(strDomain.equals(strDomains[i]))
					vReplaceDomains.add(strDomains[i]);
			}
		}
		
		
		
		
		return  null;
	}
	
	
	
	
	
	/**
	 * if the whole number has no child in the current variable, the whole will be removed
	 *  example: [1/5.2] 1 has no child. 1 will be removed
	 *  only for DTS
	 * 
	 * because I pass this during DTS: [1/1.1/1.2]
	 */
	public String generalizeDomainsDTS(String strCTValue){
		String strCTValue2 = strCTValue;
		String str = strCTValue;
		boolean bProceed = str.contains("[");
		Hashtable hReplacement = new Hashtable();
		Vector vAlreadyAdded = new Vector(1);
		
		while( bProceed){
			int nBegin = str.indexOf('[');
			int nEnd = str.indexOf(']');
			String strDomain = str.substring(nBegin+1, nEnd); // 99/22.1/99.1
			String strNewDomain = new String();
			String arrDomains[] = strDomain.split("/");
			String strCurrentWhole = new String();
			boolean bAddDecimals = true;
			boolean bWholeNumberHasChild = false;
			Vector vCurrentChild = new Vector(1);
			String strChildrenDomains = new String();
			for(int i=0; i < arrDomains.length; i++) {
				
				if(!arrDomains[i].contains(".")){
					strCurrentWhole = arrDomains[i];
				}else{
					vCurrentChild.add(arrDomains[i]);
					if (strChildrenDomains.equals("")){
						strChildrenDomains = arrDomains[i];
					}else{
						strChildrenDomains+="/"+arrDomains[i];
					}
				}
			}//end for
			
			
			
			//search vCurrentChild if any belongs to strCurrentWhole
			for(Iterator it = vCurrentChild.iterator(); it.hasNext();){
				String s = (String)it.next();
				if (TExtUtility.compareBaseDomain(strCurrentWhole, s)){
					bWholeNumberHasChild = true;
					break;
				}
			}
			
			//if yes, add whole, add all child to strNewDomain			
			if (bWholeNumberHasChild){
				strNewDomain = strCurrentWhole +"/"+strChildrenDomains;
			}else{
				strNewDomain = strChildrenDomains;
			}
			
			if (!strNewDomain.equals(""))
			strCTValue2 = strCTValue2.replace(strDomain, strNewDomain);
			
				
			str = str.substring(nEnd+1, str.length());
			bProceed = str.contains("[");
			
		}
		return strCTValue2;
	}
	
	
	
	public static void main(String[] args){
		boolean bsourceIsFil = false;
		DBConnector.connect();
		TExtLearnerUtility tlu = new TExtLearnerUtility(bsourceIsFil);
		ChunkRefiner cr = new ChunkRefiner(bsourceIsFil, tlu);
		/*
		TExtChunk cS = new TExtChunk("cook lunch with", "99");
		TExtChunk cT = new TExtChunk("magluto ng tanghalian kasama", "99");
		AlignedChunk alChunk = new AlignedChunk(cS, cT);
		AlignedSentence alSen = cr.toAlignedSentence(alChunk);
		
		//test findMatchingChunks
		System.out.println("findmatchingchunks");
		
		Vector vMatches = cr.findMatchingChunks(alChunk);
		System.out.println("sizE "+vMatches.size());
		System.out.println("Matches: " +vMatches);
		
		//means this is from DTTL
		cr.startRefinement(alChunk, null, null, new Hashtable());
		
		//means this is from TR
		//cr.startRefinement(alChunk, fakeparent, fakeparent, new Hashtable());
		System.out.println("done");
		*/
		Vector vInclude = new Vector(1);
		vInclude.add("5");

		
		System.out.println( TExtLearnerUtility.generalizeDomains("[1/99.9/99.9] [1/1.6/5.8/3.2]."));
		//System.out.println(cr.removeWholeDomainsNotInThisList("I [5/4/3/2.2] [3/1.1]", vInclude));
		System.out.println( cr.generalizeDomainsDTS("sdf [1/5.2] sdf."));
		
		System.out.println( TExtLearnerUtility.removeDuplicateDomains("[1.1/1.2/1.1/2/2] [1.2/1]."));
	}
	
	
	
	
}